Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31799 > unrolled thread

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

Started byJennie <nameDOTportua@gmail.com>
First post2012-10-20 10:05 +0200
Last post2012-10-20 10:59 +0200
Articles 7 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  How to see the __name__ attribute of a class by using dir() Jennie <nameDOTportua@gmail.com> - 2012-10-20 10:05 +0200
    Re: How to see the __name__ attribute of a class by using dir() Peter Otten <__peter__@web.de> - 2012-10-20 10:24 +0200
      Re: How to see the __name__ attribute of a class by using dir() Jennie <nameDOTportua@gmail.com> - 2012-10-20 10:59 +0200
        Re: How to see the __name__ attribute of a class by using dir() Peter Otten <__peter__@web.de> - 2012-10-20 11:43 +0200
          Re: How to see the __name__ attribute of a class by using dir() Jennie <nameDOTportua@gmail.com> - 2012-10-20 13:19 +0200
          Re: How to see the __name__ attribute of a class by using dir() Jennie <marco.buttu@gmail.com> - 2012-10-20 13:19 +0200
      Re: How to see the __name__ attribute of a class by using dir() Jennie <marco.buttu@gmail.com> - 2012-10-20 10:59 +0200

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

FromJennie <nameDOTportua@gmail.com>
Date2012-10-20 10:05 +0200
SubjectHow to see the __name__ attribute of a class by using dir()
Message-ID<k5tm0k$sph$1@speranza.aioe.org>
The dir() built-in does not show the __name__ attribute of a class:

 >>> '__name__' in Foo.__dict__
False
 >>> Foo.__name__
'Foo'

I implementd my custom __dir__, but the dir() built-in do not want to 
call it:

 >>> class Foo:
...     @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
-- 
Jennie

[toc] | [next] | [standalone]


#31800

FromPeter Otten <__peter__@web.de>
Date2012-10-20 10:24 +0200
Message-ID<mailman.2554.1350721484.27098.python-list@python.org>
In reply to#31799
Jennie wrote:

> The dir() built-in does not show the __name__ attribute of a class:
> 
>  >>> '__name__' in Foo.__dict__
> False
>  >>> Foo.__name__
> 'Foo'
> 
> I implementd my custom __dir__, but the dir() built-in do not want to
> call it:
> 
>  >>> class Foo:
> ...     @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:

>>> class Foo(object):
...     def __dir__(self): return ["python"]
... 
>>> dir(Foo())
['python']

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']

[toc] | [prev] | [next] | [standalone]


#31802

FromJennie <nameDOTportua@gmail.com>
Date2012-10-20 10:59 +0200
Message-ID<508267F8.5030302@gmail.com>
In reply to#31800
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__', ...]

Regards,
-- 
Jennie

[toc] | [prev] | [next] | [standalone]


#31805

FromPeter Otten <__peter__@web.de>
Date2012-10-20 11:43 +0200
Message-ID<mailman.2557.1350726214.27098.python-list@python.org>
In reply to#31802
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']

[toc] | [prev] | [next] | [standalone]


#31808

FromJennie <nameDOTportua@gmail.com>
Date2012-10-20 13:19 +0200
Message-ID<508288A9.807@gmail.com>
In reply to#31805
On 10/20/2012 11:43 AM, Peter Otten wrote:

> 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']

Thanks! :)


-- 
Jennie

[toc] | [prev] | [next] | [standalone]


#31809

FromJennie <marco.buttu@gmail.com>
Date2012-10-20 13:19 +0200
Message-ID<mailman.2559.1350731951.27098.python-list@python.org>
In reply to#31805
On 10/20/2012 11:43 AM, Peter Otten wrote:

> 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']

Thanks! :)


-- 
Jennie

[toc] | [prev] | [next] | [standalone]


#31803

FromJennie <marco.buttu@gmail.com>
Date2012-10-20 10:59 +0200
Message-ID<mailman.2555.1350723588.27098.python-list@python.org>
In reply to#31800
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__', ...]

Regards,
-- 
Jennie

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web