Last updated: Apr 9, 2024
Reading timeยท4 min
To run a function only once:
False
.True
in the function.False
.sum_has_run = False def sum(a, b): global sum_has_run if sum_has_run: return sum_has_run = True return a + b print(sum(100, 100)) # ๐๏ธ 200 print(sum(100, 100)) # ๐๏ธ None
The first example uses a global variable to run a function only once.
False
and set its value to True
in the function.If the value of the global variable is equal to True
, we return from the
function straight away.
sum_has_run = False def sum(a, b): global sum_has_run if sum_has_run: return sum_has_run = True return a + b print(sum(100, 100)) # ๐๏ธ 200 print(sum(100, 100)) # ๐๏ธ None
True
, we set its value to True
and run the function.This only happens the first time the function is invoked.
Once the global variable is set to True
, the function
returns None without running any code.
An alternative approach is to use an attribute on the function.
This is a three-step process:
has_run
attribute on the function to True
the first time it runs.has_run
attribute is True
and return straight away.def sum(a, b): if getattr(sum, 'has_run', False): return sum.has_run = True return a + b print(sum(100, 100)) # ๐๏ธ 200 print(sum(100, 100)) # ๐๏ธ None
We set the has_run
attribute to True
the first time the function is called.
The getattr function returns the value of the provided attribute of the object.
The function takes the object, the name of the attribute and a default value for when the attribute doesn't exist on the object as parameters.
If the has_run
attribute is not set on the function, we return a default value
of False
.
has_run
attribute each time the function runs and if its value is set to True
, we return from the function straight away.If the has_run
attribute is not set, the code in the function runs and sets
the attribute to True
.
The code in the function is only run once.
You can run a loop only once by using the range()
class with a stop
value of
1
.
for i in range(1): print('Loop is only run once')
The range() class is commonly used for looping
a specific number of times in for
loops and takes the following arguments:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range will consist of every N numbers from start to stop (defaults to 1 ) |
If you only pass a single argument to the range()
constructor, it is
considered to be the value for the stop
parameter.
for n in range(1): print(n) # ๐๏ธ 0 result = list(range(1)) # ๐๏ธ [0] print(result)
The for
loop in the example only runs once.
You can also use a while True
loop to only run a loop once.
while True: print('Loop is only run once') break
A while True
loop can only be stopped by using the break
statement.
The break statement breaks out of the
innermost enclosing for
or while
loop.
You can also use the break
statement to run a loop only once when iterating
over a sequence.
a_list = ['bobby', 'hadz', 'com'] for item in a_list: print(item) # ๐๏ธ bobby break
The loop only prints the first item of the list and exits.
You can also use a boolean variable to run a loop only once.
a_list = ['bobby', 'hadz', 'com'] has_run = False for item in a_list: if has_run: break has_run = True print(item) # ๐๏ธ bobby
We initialized the has_run
variable to False
.
The first time the for
loop runs, we set the variable to True
.
The next time the loop runs, the if
condition is met and we exit out of the
loop.
I've also written an article on how to call a function N times.
If you need to call a function by a string name, click on the link and follow the instructions.
You can learn more about the related topics by checking out the following tutorials: