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

Nathaniel Smith njs at pobox.com
Wed Jun 14 21:06:02 EDT 2017


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. (And of
course if you wanted to get really fancy and handle cases where your
object inherits from some type other than 'object', or where some user
sticks your type into a multiple-inheritance hierarchy, you might
potentially want to find "__dir__" using super lookup instead of going
directly to PyBaseObject_Type. From a quick google it looks like this
page gives an approach for doing that:
https://pythonextensionpatterns.readthedocs.io/en/latest/super_call.html)

-n

-- 
Nathaniel J. Smith -- https://vorpus.org


More information about the Python-ideas mailing list