[Python-ideas] the error that raises an AttributeError should be passed to __getattr__

Ethan Furman ethan at stoneleaf.us
Mon Jun 19 22:12:23 EDT 2017


On 06/19/2017 06:31 PM, Chris Angelico wrote:
> On Tue, Jun 20, 2017 at 10:18 AM, Steven D'Aprano <steve at pearwood.info> wrote:
>> Having said that, there's another problem: adding this feature (whatever
>> it actually is) to __getattr__ will break every existing class that uses
>> __getattr__. The problem is that everyone who writes a __getattr__
>> method writes it like this:
>>
>>      def __getattr__(self, name):
>>
>> not:
>>
>>      def __getattr__(self, name, error):
>>
>> so the class will break when the method receives two arguments
>> (excluding self) but only has one parameter.
>
> Why not just write cross-version-compatible code as
>
> def __getattr__(self, name, error=None):
>
> ? Is there something special about getattr?

The point was existing code would fail until that change was made.  And a lot of existing code uses __getattr__.

--
~Ethan~



More information about the Python-ideas mailing list