Python: source code has attributes that are unaccessible from outside of it

Viewed 94

So i've been wondering around the other day and stumbled across this piece of code from datetime.datetime:

def timestamp(self):
    "Return POSIX timestamp as float"
    if self._tzinfo is None:
        s = self._mktime()
        return s + self.microsecond / 1e6
    else:
        return (self - _EPOCH).total_seconds()

I decided to check what self._tzinfo and self._mktime() are, so i ran this code in console:

>>> datetime(1970, 1, 1)._tzinfo
Traceback (most recent call last):
   File "<input>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute '_tzinfo'
>>> datetime(1970, 1, 1)._mktime()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute '_mktime'

But to my surprise i got AttributeError on both occasions. PyCharm says Cannot find declaration to go to. Yet, there is a normal declaration of _mktime() method right above the timestamp() method.

Despite all this, of course, timestamp() works totally fine.

>>> datetime(1970, 1, 1).timestamp()
-10800.0

So why is this happening? Why are these attributes not accessible from outside and why does interpreter react to them so weirdly?

0 Answers
Related