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


Groups > comp.lang.python > #31800

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'modify': 0.05; '__name__': 0.07; 'problem?': 0.07; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:()': 0.09; 'subject:using': 0.09; 'advance': 0.10; 'def': 0.10; '...]': 0.16; '@classmethod': 0.16; 'class:': 0.16; 'foo(object):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:class': 0.16; 'wrote:': 0.17; 'implementing': 0.17; 'instance': 0.17; '>>>': 0.18; 'skip:_ 20': 0.22; 'header:User- Agent:1': 0.26; 'skip:[ 10': 0.26; 'header:X-Complaints-To:1': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; "skip:' 10": 0.30; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'built-in': 0.35; 'false': 0.35; 'received:org': 0.36; 'but': 0.36; 'method': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'show': 0.63; "'foo'": 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How to see the __name__ attribute of a class by using dir()
Date Sat, 20 Oct 2012 10:24:36 +0200
Organization None
References <k5tm0k$sph$1@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849431.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2554.1350721484.27098.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350721484 news.xs4all.nl 6924 [2001:888:2000:d::a6]:42401
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31800

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web