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

Barry Scott barry at barrys-emacs.org
Wed Jun 14 16:54:11 EDT 2017


> On 13 Jun 2017, at 23:49, Chris Angelico <rosuav at gmail.com> wrote:
> 
> On Wed, Jun 14, 2017 at 8:09 AM, Terry Reedy <tjreedy at udel.edu> wrote:
>> Perhaps you are looking for __dir__, called by dir().
>> 
>> " dir([object])
>> 
>>    Without arguments, return the list of names in the current local scope.
>> With an argument, attempt to return a list of valid attributes for that
>> object.
>> 
>>    If the object has a method named __dir__(), this method will be called
>> and must return the list of attributes."
> 
> AIUI the OP is looking to implement __dir__, but make use of *what
> dir() would have returned* in that function. Something like:

Yes.

> 
> class Magic:
>    def __getattr__(self, attr):
>        if attr in self.generatables:
>            return self.generated_value(attr)
>        raise AttributeError
>    def __dir__(self):
>        return default_dir(self) + self.generatables
> 
> 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.

Today I solve the problem in 2.7 C extension code by providing a value for __members__.
In python3 I have no idea how to do this in C.

I can find no example code that addresses this problem.

How am I supposed to code this without the __members__ trick?
Did I miss the C API that implements default_dir(self)?

Barry


> 
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
> 



More information about the Python-ideas mailing list