I have a variable which may or may not get a value in the instance:
class EC():
__init__(self, a=False):
...
if a: self.__var = ...
Later I want to check if the __var exists in the instance. Because prepending __ to the name changes the internal name to _EC__var the checking code becomes a little bit messy:
if ''.join(['_',self.__class__.__name__,'__name']) in self.__dict__: ...
Is code above considered normal or not? If not what are the preferred alternatives?
One option I can think of is to give __var some value anyway, for example:
_no_value = object()
...
def __init__(self, a):
self.__var = _no_value
...
if a: self.__var = ...
So later I can compare __var to _no_value instead of a mess with internal variables.