Why can't I access the private variables of the superclass in Python?
private-members, python, python-3.x, subclass
Solution
if you look at the end of `datetime.py`, you'll see this:
try:
from _datetime import *
except ImportError:
pass
this imports among other things the C-version of the previously defined python classes, which will therefore be used, and those don't have the members you're trying to access.
Problem
I know that I should use the access methods. I see in the `datetime` module that the class `datetime` inherits from date. ``` class datetime(date): <some other code here....> self = date.__new__(cls, year, month, day) self._hour = hour self._minute = minute self._second = second self._microsecond = microsecond self._tzinfo = tzinfo return self ``` I also see that datetime is able to access the members of date, as in `__repr__`: ``` def __repr__(self): """Convert to formal string, for repr().""" L = [self._year, self._month, self._day, # These are never zero self._hour, self._minute, self._second, self._microsecond] ``` I tried to subclass datetime to add some information to it and then write a similar `__repr__` function: ``` def __repr__(self): """Convert to formal string, for repr().""" L = [self._year, self._month, self._day, # These are never zero self._hour, self._minute, self._second, self._microsecond, self._latitude, self._longitude] ``` The debugger complained that self._year didn't exist. (`self.year` works, however.) I know that I should be using the access function. I just want to understand why `datetime` is able to access the private variables of `date` but my subclass isn't able.