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

Ethan Furman ethan at stoneleaf.us
Sun Jan 26 23:49:41 CET 2014


On 01/26/2014 02:04 PM, mark gross wrote:
> On Sun, Jan 26, 2014 at 12:16:37PM -0800, Ethan Furman wrote:
>> On 01/26/2014 12:06 PM, mark gross wrote:
>>>
>>> But, I'd like to ask the pdx-python crew about uses of __getattrib__.
>>
>> There is no __getattrib__ -- there is a __getattr__ and a __getattribute__; which did you mean?
>
> Sorry for the type-oh.  I'm talking about __getattr__

Okay, I read your blog entry -- pretty nice!

And yes, __getattr__ is at the tail-end of lookup attempts that Python will perform when trying to find an attribute on 
an object, and yes, you can do juts about anything there.  The only thing more powerful than __getattr__ is 
__getattribute__ (because __getattribute__ is *always* called, while __getattr__ is only called when an attribute cannot 
be found).

The most common use for __getattr__ is to allow virtual attributes, and the way git-python is using it is normal.

Because __getattr__ can be a bit of a nuisance for introspection other helper-type methods have been added, such as 
__dir__ (so git-python could specify in its __dir__ method the various command-line converters it allowed);  also, 
descriptors (such as property) are encouraged where appropriate.

--
~Ethan~


More information about the Portland mailing list