[Python-ideas] Restore the __members__ behavior to python3 for C extension writers

Nick Coghlan ncoghlan at gmail.com
Wed Jun 14 23:45:13 EDT 2017


On 15 June 2017 at 11:06, Nathaniel Smith <njs at pobox.com> wrote:
> On Wed, Jun 14, 2017 at 1:54 PM, Barry Scott <barry at barrys-emacs.org> wrote:
>> > On 13 Jun 2017, at 23:49, Chris Angelico <rosuav at gmail.com> wrote:
>> > For that purpose, is it possible to use super().__dir__()? Are there
>> > any considerations where that would fail?
>>
>> Remember that I need to do this in the C API and I want default_dir of self in C not python.
>>
>> super().__dir__ looks at the class above me that is typically object() and so is not useful
>> as it does not list the member function from my class or __mro__ or other stuff I may not be aware of
>> that is important to return.
>
> object.__dir__(your_class_instance) should generally return everything
> you would get if you didn't override __dir__ at all. Remember, that
> code doesn't mean "return the methods and attributes defined on the
> object class", it's "run the object class's __dir__ method with
> self=your_class_instance".
>
> I don't know off-hand if there's a nicer way to do this from C than to
> manually look up the "__dir__" attribute on PyBaseObject_Type.

This is the kind of case where
https://docs.python.org/3/c-api/object.html#c.PyObject_CallMethod is
useful:

    dir_result = PyObject_CallMethod(base_type, "__dir__", "O", self);
    /* Add any additional attributes to the dir_result list */
    return dir_result;

Fully supporting multiple inheritance is more work (as your link
shows), and often not needed.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list