dunder-docs (was Python is DOOMED! Again!)

Devin Jeanpierre jeanpierreda at gmail.com
Mon Feb 2 02:41:31 EST 2015


-- Devin

On Sun, Feb 1, 2015 at 11:15 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Gregory Ewing wrote:
>
>> Steven D'Aprano wrote:
>>>     [quote]
>>>     If the object has a method named __dir__(), this method will
>>>     be called and must return the list of attributes.
>>>     [end quote]
>>>
>>> The first inaccuracy is that like all (nearly all?) dunder methods,
>>> Python only looks for __dir__ on the class, not the instance itself.
>>
>> It says "method", not "attribute", so technically
>> it's correct. The methods of an object are defined
>> by what's in its class.
>
> Citation please. I'd like to see where that is defined.

https://docs.python.org/3/glossary.html#term-method

> Even if it is so defined, the definition is wrong. You can define methods on
> an instance. I showed an example of an instance with its own personal
> __dir__ method, and showed that dir() ignores it if the instance belongs to
> a new-style class but uses it if it is an old-style class.

You didn't define a method, you defined a callable attribute.
Old-style classes will call those for special method overriding,
because it's the simplest thing to do. New-style classes look methods
up on the class as an optimization, but it also really complicates the
attribute semantics. The lookup strategy is explicitly defined in the
docs. pydoc is, like always, incomplete or inaccurate. See
https://docs.python.org/2/reference/datamodel.html#special-method-names

-- Devin



More information about the Python-list mailing list