I have a command line utility which can accept various commands what to do, so basically there is a big elif chain what command will be executed. Some of the commands use variable (lets call it A
) so it has to be initialized with a value. So normally I would do:
self.A = createContent()
doSomething(self.A.B)
Now I don't want to put self.A = createContent()
for every command where the A
is needed so I would like to ask if there is a way in Python how to initialize the variable automatically when its None
for example. The createContent
takes time to process so I don't want to put it into constructor which gets executed for every command.
self
, and couldn't you just use that object's initialisation method to defineA
?A
is sometimes needed, and sometimes not at all. I don't want to use object's initialization method because defining A is expensive and it is not always needed. At the same time I don't want to do checking whether it is not defined and define it on all the places where theA
is needed.