Use default argument if argument is None on python method call

From your updated question with the classes, the easy way is just to alias self.h.x as y:

class Open(object):
    def __init__(self):
        self.h = Hidden()
        self.y = self.h.x

and now when you call o.y(...) you're actually just calling Hidden.x:

o = Open()
o.y("Hello") # > Hello John Doe

o.y("Hello", "Mister X") # > Hello Mister X

If you can modify x:

def x(a, b=None):
    b = "John Doe" if b is None else b 
    print a, b

If you want to get rid of 'if' just do

self.h.x(a, [b, 'Default Value'][b is None])

ps. this works in python3.5+ and i don't care about python2.7

Tags:

Python