This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author denis-osipov
Recipients denis-osipov, docs@python, steven.daprano
Date 2018-11-05.09:45:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1541411147.47.0.788709270274.issue35165@psf.upfronthosting.co.za>
In-reply-to
Content
Got it.

But now docs says that overriding the __getattr__() method is enough to customize attribute access. It's not completely true.

If I understand it correct, to make __getattr__() work every time you need to call it by __getattribute__ or raise AttributeError, e.g. store attributes somewhere not in instance __dict__. In this case you need to override __setattr__ too.

class MyGetattrClass:
    def __init__(self):
        super().__setattr__("attrs", {})

    def __setattr__(self, name, value):
        self.attrs[name] = value

    def __getattr__(self, name):
        try:
            print(f"{name} equals {self.attrs[name]}")
        except KeyError:
            raise AttributeError(
                f"{type(self).__name__!r} object has no attribute {name!r}"
            ) from None

If it's correct, we probably should add some clarification in expressions doc. Or maybe just link to https://docs.python.org/3/reference/datamodel.html#object.__getattr__ (which mention about it) will be enough.
History
Date User Action Args
2018-11-05 09:45:47denis-osipovsetrecipients: + denis-osipov, steven.daprano, docs@python
2018-11-05 09:45:47denis-osipovsetmessageid: <1541411147.47.0.788709270274.issue35165@psf.upfronthosting.co.za>
2018-11-05 09:45:47denis-osipovlinkissue35165 messages
2018-11-05 09:45:47denis-osipovcreate