[portland] __gitattrib__ idoms. How often are they used?

Ethan Furman ethan at stoneleaf.us
Mon Jan 27 18:14:12 CET 2014


On 01/26/2014 07:43 PM, Christopher Hiller wrote:
>
>--> class Bar(Foo):
>...
>...       def __init__(self, codec, d=None, **kwargs):
>...           ...
>...
>...       @property
>...       def __codec__(self):
>...           ...
>...
>...       def __getattr__(self, name):
>...           ...
>...
>...       def __setattr__(self, name, value):
>...           ...
>...
>--> bar = Bar('rot13', beavis='butthead')
>--> bar.__codec__ = 'foo'
>
> The |bar.__codec__ = 'foo'| is especially weird; it doesn't raise an exception but feels like it should. Couldn't figure
> out how to get that to happen [...]

The issue here is that, similarly to __getattribute__, __setattr__ is /always/ called; while object.__setattr__ is smart 
enough to deal with descriptors such as @property, most other __setattr__s remain blissfully unaware.

So the standard __setattr__ dance is something like:

     def __setattr__(self, name, value):
         if name in ('things', 'I', "don't", 'want', 'to', 'deal', 'with'):
             return super(MyClass, self).__setattr__(name, value)
         # deal with the stuff I do care about here

--
~Ethan~


More information about the Portland mailing list