import sched, time
s = sched.scheduler(time.time, time.sleep)
def do_something(sc):
print("Doing stuff...")
s.enter(5, 1, do_something, argument=(sc,))
s.enter(5, 1, do_something, argument=(s,))
s.run()
In this code bit, I am using sched lib to create a function loop. Here I noticed that when we are assigning an argument, there is a trailing comma. I am wondering why is that?
When I delete the trailing comma, it returns error says:
do_something() argument after * must be an iterable, not scheduler
In other trailing comma questions, people said trailing comma will turns int into a tuple. Here, when I add the trailing comma or not, the value type of s is still scheduler object.
Tuples only need , commas, they don't need () parenthesis, except when they do! So for example in:
x = 1, 2, 3
y = (1, 2, 3)
And:
x = 1,
y = (1,)
In both cases x and y are identical. But in your case:
s.enter(5, 1, do_something, argument=(sc,))
s.enter(5, 1, do_something, argument=sc,)
Are, as you found out, not the same. Why?
Well in the second case argument=sc, the , is interpreted to be part of the ,'s of the parameter list, so it is not considered as indicating a tuple(). And in this case, you need the (sc,) to build a tuple()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With