multiple default values for function arguments Python -


if had class function following , wanted have 2 variables had default values:

class item:     def __init__(self, var1=1, var2=2)         self.var1 = var1         self.var2 = var2 

how create instance of class , change var2's value not var1's?

a = item(#leave var1 alone, set var2 'hello') 

you instantiate like:

# leave var1 default = item(var2='hello')  # leave var2 default = item(var1='hey')  # overwrite both var1 , var2 = item(var1='hey', var2='hello')  # use both defaults = item() 

the thing note in example in comment must provide var1 since has no default. item(var1=1, var3=2) , item(1, var3=2) both work, while item(var3=2) not. keyword arguments must come last when there arguments without default values, item(var3=2, 1) not work item(var3=2, var1=1) would.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -