How to see the __name__ attribute of a class by using dir()

J

Jennie

The dir() built-in does not show the __name__ attribute of a class:
'Foo'

I implementd my custom __dir__, but the dir() built-in do not want to
call it:
.... @classmethod
.... def __dir__(cls):
.... return ['python']
....
Foo.__dir__() ['python']
dir(Foo)
['__class__', '__delattr__', '__dict__', ...]

Can someone tell me where is the problem? Thanks a lot in advance
 
P

Peter Otten

Jennie said:
The dir() built-in does not show the __name__ attribute of a class:
'Foo'

I implementd my custom __dir__, but the dir() built-in do not want to
call it:
... @classmethod
... def __dir__(cls):
... return ['python']
...
Foo.__dir__() ['python']
dir(Foo)
['__class__', '__delattr__', '__dict__', ...]

Can someone tell me where is the problem? Thanks a lot in advance

Implementing __dir__ as an instance method works:
.... def __dir__(self): return ["python"]
....['python']

So if you want to customise dir(Foo) you have to modify the metaclass:
.... class __metaclass__(type):
.... def __dir__(self): return ["python"]
....['python']
 
J

Jennie

So if you want to customise dir(Foo) you have to modify the metaclass:
... class __metaclass__(type):
... def __dir__(self): return ["python"]
...['python']

Hi Peter, thanks for your answer, but it does not work (Python 3.3):
.... class __metaclass__(type):
.... def __dir__(self): return ["python"]
....['__class__', '__delattr__', '__dict__', '__dir__', ...]

Regards,
 
J

Jennie

So if you want to customise dir(Foo) you have to modify the metaclass:
... class __metaclass__(type):
... def __dir__(self): return ["python"]
...['python']

Hi Peter, thanks for your answer, but it does not work (Python 3.3):
.... class __metaclass__(type):
.... def __dir__(self): return ["python"]
....['__class__', '__delattr__', '__dict__', '__dir__', ...]

Regards,
 
P

Peter Otten

Jennie said:
So if you want to customise dir(Foo) you have to modify the metaclass:
class Foo:
... class __metaclass__(type):
... def __dir__(self): return ["python"]
...
['python']

Hi Peter, thanks for your answer, but it does not work (Python 3.3):
... class __metaclass__(type):
... def __dir__(self): return ["python"]
...['__class__', '__delattr__', '__dict__', '__dir__', ...]

In Python 3 the way to specify the metaclass has changed:
.... def __dir__(self): return ["python"]
........ pass
....['python']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top