Small Python optional arguments question

Posted by ooboo on Stack Overflow See other posts from Stack Overflow or by ooboo
Published on 2009-07-13T09:23:48Z Indexed on 2010/05/23 22:10 UTC
Read the original article Hit count: 292

I have two functions:

def f(a,b,c=g(b)):
    blabla

def g(n):
    blabla

c is an optional argument in function f. If the user does not specify its value, the program should compute g(b) and that would be the value of c. But the code does not compile - it says name 'b' is not defined. How to fix that?

Someone suggested:

def g(b):
    blabla

def f(a,b,c=None):
    if c is None:
        c = g(b)
    blabla

But this doesn't work, because maybe the user intended c to be None and then c will have another value.

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner