This is not a problem I've encountered, but rather just a curiosity about best practices/style in python:
I see stuff like this quite often:
class A:
def __init__(self, qux):
"""...do whatever... maybe this is expensive - maybe it isn't - maybe I don't know if it's expensive"""
def get_baz(foo, bar=A(2)):
"""...do another thing..."""
return baz
I largely avoid function/method calls (like bar=A(2)
) in function signatures, because this makes A.__init__
run on import. Do you think avoiding the running of code on import is best practice, or am I just OCD and it's totally fine to handle this on a case-by-case basis? I feel like there is an expectation (at least in the python community) that importing a package/module shouldn't take a significant amount of time/memory.
The common alternative to the above is slightly more verbose, but runs no extra code on import:
def get_baz(foo, bar=None):
if bar is None:
foo = A()
Which do you like more and why? Should I just not care?
(For context, I started thinking about this because importing an internal package at my company took 8 seconds, which was triggering.)