I was teaching someone about how the
__new__ method is used in python code by showing them the
datetime module, and I came across a behavior that I was surprised by:
>>> from datetime import datetime >>> foo = datetime.now() >>> d.day 28 >>> d._day Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'datetime.datetime' object has no attribute '_day'
From reading the source code of the
datetime module, I know that
day is a
property that reads the data stored on the
My surprise is that when I implement the same pattern in my own classes, I can access the variables with a single underscore.
With this behavior, I would expect to see a customized
__getattribute__ method, but there are none to be found in the module.
Does this have to do with an underlying implementation in C code, or is there something additional going on here that I have not yet discovered?