Python function optional arguments - possible to add as condition? -


is possible, somehow, aassign condtional statement toan optional argument?

my initial attempts, using following construct, have been unsuccessful:

y = {some value} if x == {somevalue} else {anothervalue} 

where x has assigned beforehand.

more specifically, want function signature like:

def x(a, b = 'a' if somemodule.somefunction() else somemodule.someotherfunction()):    :    : 

many thanks

sure, that's how it. may want keep in mind b's default value set after define function, may not desirable:

def test():     print("i'm called once")     return false  def foo(b=5 if test() else 10):     print(b)  foo() foo() 

and output:

i'm called once 10 10 

just because possible doesn't mean should it. wouldn't, @ least. verbose way using none placeholder easier understand:

def foo(b=none):     if b none:         b = 5 if test() else 10      print b 

Comments