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

Peter Otten __peter__ at web.de
Sat Oct 20 05:43:25 EDT 2012


Jennie wrote:

> On 10/20/2012 10:24 AM, Peter Otten wrote:
> 
>> So if you want to customise dir(Foo) you have to modify the metaclass:
>>
>>>>> >>>class Foo:
>> ...     class __metaclass__(type):
>> ...             def __dir__(self): return ["python"]
>> ...
>>>>> >>>dir(Foo)
>> ['python']
>>
>>
> 
> Hi Peter, thanks for your answer, but it does not work (Python 3.3):
> 
>  >>> class Foo:
> ...     class __metaclass__(type):
> ...         def __dir__(self): return ["python"]
> ...
>  >>> dir(Foo)
> ['__class__', '__delattr__', '__dict__', '__dir__', ...]

In Python 3 the way to specify the metaclass has changed:

>>> class FooType(type):
...     def __dir__(self): return ["python"]
... 
>>> class Foo(metaclass=FooType):
...     pass
... 
>>> dir(Foo)
['python']





More information about the Python-list mailing list