This is the most pythonic way:
def foo(arg=None): if arg is None: arg = bar() ...
If you want the bar function to be called only once, and you do not want it to be called during import, then you will need to maintain this state somewhere. Perhaps in the called class:
class Foo: def __init__(self): self.default_arg = None def __call__(self, arg=None): if arg is None: if self.default_arg is None: self.default_arg = bar() arg = self.default_arg ... foo = Foo()
source share