Why not assign a to b in the first line of the function?
- McGlothlin
You can't do that, you need to do b=None then if b is None: b = a. See stackoverflow.com/q/4575326/3001761 (which you could have found trivially if you'd searched for this...)
- jonrsharpe
1 Answers
20
votes
No. This is not possible. The Python interpreter thinks that you want to assign the default value of argument b to a global variable a when there isn't a global variable a.
You might want to try something like this:
def func(a, b=None):
if b is None:
b = a
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
atobin the first line of the function? - McGlothlinb=Nonethenif b is None: b = a. See stackoverflow.com/q/4575326/3001761 (which you could have found trivially if you'd searched for this...) - jonrsharpe