Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'python,': 0.01; 'assign': 0.04; 'instance,': 0.05; 'attribute': 0.07; 'subject:object': 0.07; 'python': 0.08; '>>>>': 0.09; '__name__': 0.09; 'attribute.': 0.09; 'descriptor': 0.09; 'dynamically': 0.09; 'none:': 0.09; 'overwrite': 0.09; 'am,': 0.12; 'stored': 0.13; 'def': 0.15; 'class,': 0.15; 'skip:" 40': 0.15; '(instance': 0.16; '@property': 0.16; '__init__': 0.16; 'attribute,': 0.16; 'bases,': 0.16; 'descriptor,': 0.16; 'instances.': 0.16; 'lookup': 0.16; 'metaclass': 0.16; 'needless': 0.16; 'non-data': 0.16; 'obj,': 0.16; 'objects).': 0.16; 'url:voidspace': 0.16; 'cc:addr:python- list': 0.16; 'thanks!': 0.16; 'wrote:': 0.16; 'wed,': 0.17; '\xa0so': 0.18; 'cc:no real name:2**0': 0.20; '(most': 0.21; 'maybe': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'aug': 0.24; 'traceback': 0.24; 'guess': 0.26; 'string': 0.26; '(the': 0.28; 'classes': 0.28; 'skip:" 30': 0.28; 'url:mailman': 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'example': 0.30; 'module': 0.30; 'typeerror:': 0.30; 'thanks': 0.30; 'class': 0.30; 'equivalent': 0.31; 'subject:?': 0.31; "didn't": 0.31; 'michael': 0.31; 'error': 0.32; 'actual': 0.32; 'objects': 0.32; "isn't": 0.33; 'url:listinfo': 0.33; 'calling': 0.33; 'however,': 0.34; '...': 0.34; 'eric': 0.34; 'last):': 0.34; 'uses': 0.35; 'file': 0.36; 'url:python': 0.36; 'skip:" 10': 0.36; 'class.': 0.37; 'but': 0.37; 'something': 0.37; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'data': 0.39; 'where': 0.40; 'thinking': 0.40; 'provided': 0.60; 'your': 0.61; 'property': 0.63; 'link': 0.63; 'believe': 0.65; 'making': 0.67; 'received:209.85.215.174': 0.67; 'received:mail- ey0-f174.google.com': 0.67; 'have.': 0.77; 'elaborate': 0.84; 'moment.': 0.84; 'staring': 0.84; 'snow': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=1iLaQTmArx9jBhK07SpNNNUkBnDUkz8YpmHZS5FGdxU=; b=qISUuVpjpQs7xgJ0VY/Y1BZvpSlF8Re+O/CXF9WSPbfsoWgVx8ZdXX0VAn58Ddk5K4 gwI+vGNvTHl3ajQIOZmKS6xR7UbTA9AUyOE/8NW6YuHJOel5h56CfG5ZLCgZ6W9rQ6UU 60W7k2TneJEowX9Bs9HAyjPUlCPbzD/fsjL6o= MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 10 Aug 2011 10:54:54 -0600 Subject: Re: how to dynamically generate __name__ for an object? From: Eric Snow To: Fuzzyman Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 81 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312995302 news.xs4all.nl 23935 [2001:888:2000:d::a6]:45896 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11144 On Wed, Aug 10, 2011 at 8:48 AM, Fuzzyman wrote: > On Aug 7, 4:06=A0am, Eric Snow wrote: >> Thought I knew how to provide a dynamic __name__ on instances of a >> class. =A0My first try was to use a non-data descriptor: >> >> # module base.py >> >> class _NameProxy(object): >> =A0 =A0 def __init__(self, oldname): >> =A0 =A0 =A0 =A0 self.oldname =3D oldname >> =A0 =A0 def __get__(self, obj, cls): >> =A0 =A0 =A0 =A0 if obj is None: >> =A0 =A0 =A0 =A0 =A0 =A0 return self.oldname >> =A0 =A0 =A0 =A0 if "__name__" not in obj.__dict__: >> =A0 =A0 =A0 =A0 =A0 =A0 return str(obj.__context__) >> =A0 =A0 =A0 =A0 return obj.__name__ >> >> class _BaseMeta(type): >> =A0 =A0 def __init__(cls, name, bases, namespace): >> =A0 =A0 =A0 =A0 cls.__name__ =3D _NameProxy(name) >> >> class Base(object): >> =A0 =A0 __metaclass__ =3D _BaseMeta >> >> $ python _base.py >> Traceback (most recent call last): >> =A0 ... >> =A0 File "/usr/local/lib/python2.4/site-packages/base.py", line xx, in _= _init__ >> =A0 =A0 cls.__name__ =3D _NameProxy(name) >> TypeError: Error when calling the metaclass bases >> =A0 =A0 can only assign string to Base.__name__, not '_NameProxy' >> >> Needless to say I was surprised. =A0After looking in typeobject.c, I >> believe that __name__ must be a string where classes are concerned[1]. >> =A0So if I want all my instances to have a __name__ attribute, and for >> it to be dynamically provided if it isn't set on the instance, what >> are my options? =A0Or maybe I did something wrong and it should work as >> I expected? >> > > __name__ can be a descriptor, so you just need to write a descriptor > that can be fetched from classes as well as instances. > > Here's an example with a property (instance only): > >>>> class Foo(object): > ... =A0 @property > ... =A0 def __name__(self): > ... =A0 =A0 return 'bar' > ... >>>> Foo().__name__ > 'bar' Thanks! Your example is exactly what I didn't try in the first place, but should have. I was thinking of __name__ on a class as a simple data attribute. My original worry was that by adding a descriptor for __name__ on my class, I would overwrite the name of the class, hence the elaborate descriptor. However, for type objects (classes) __name__ is a data descriptor and the actual name isn't stored in __name__. I was staring that right in the face (the link I provided is for the setter method of the __name__ "property" for type objects). I guess it was a "forest for the trees" moment. Because of attribute lookup in Python, "Base.__name__" uses the equivalent of "Base.__class__.__name__.__get__(..., Base, Base.__class__)". Anyway, thanks for making me see how dumb I am! -eric > > Michael > -- > http://voidspace.org.uk/ > -- > http://mail.python.org/mailman/listinfo/python-list >