Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trailing comma in assigning argument parameter in 'sched.scheduler.enter'

Tags:

python

tuples

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.

like image 531
Runkun Miao Avatar asked Sep 08 '25 02:09

Runkun Miao


1 Answers

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()

like image 130
Stephen Rauch Avatar answered Sep 09 '25 15:09

Stephen Rauch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!