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

Chris Angelico rosuav at gmail.com
Tue Jun 13 18:49:43 EDT 2017


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:

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?

ChrisA


More information about the Python-ideas mailing list