I wrote the code which does the first part of task: it returns the count of calls of function
def counter():
val = [0]
def generate_count():
val[0] = 1
return val[0]
return generate_count
generate_count = counter()
print(generate_count())
But also I must be able to send start value of count into the func. As I know if u enter in ur function that u need to pass arguments then u are not be able to leave value blank. For example: If u write like this
def func1(n):
then you must pass one argument to make it work otherwise it wont work, so how to make it possible to work if u pass any arguments else if u don`t?
CodePudding user response:
Use a default value:
def func1(n=0):
<code>
This function can be called with a parameter n
, or it can be called without n
. If you call the function like this:
func1()
The value of the parameter n
will be set to 0, as you didn't specify a value.
CodePudding user response:
It would probably make more sense to use a builtin generator for this case.
def counter(n=0):
while True:
n = 1
yield n
generate_count = counter(1)
print(next(generate_count)) # > 2