What's the best way to check if class instance variable is set in Python?
You've forgotten the EAFP principle:
try:
value = self.__var
except AttributeError:
# do something else
If you're determined to use a sentinel, you can combine it with a class variable:
class EC():
__var = object():
...
if self.__var is not EC.__var:
...
Just use hasattr(self, '_var') to see if it exists - it may be set to None but it will exist if hasattr says it does.
E.g.:
>>> class a():
... def __init__(self):
... self.a = 3
... self._a_ = 4
... self.__a__ = 'Fred'
...
>>> A=a()
>>> hasattr(a, 'a')
False
>>> hasattr(A, 'a')
True
>>> hasattr(A, '_a_')
True
>>> hasattr(A, '__a__')
True
>>> hasattr(A, '__b__')
False
>>>
Just set it to None
on the class:
class EC():
__var = None
__init__(self, a=False):
...
if a: self.__var = ...
then test for if self.__var is not None
.
If None
should be a valid value for the attribute, use a different singleton sentinel:
_sentinel = object()
class EC():
__var = _sentinel
__init__(self, a=False):
...
if a: self.__var = ...
and test for if self.__var is not _sentinel
.
This way, all references to __var
are properly rewritten to include the class name.
The other path would be to not use double-underscore names for your attributes. __var
should only be used for attributes you want to namespace to your specific class so that subclasses do not accidentally clobber it with their own attributes.
In other words, do not use double-underscore names unless you really understand what they are for and actually need it. Any code that is not part of a framework for wider consumption by unknown third parties? Just stick to single underscores instead.