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


Groups > comp.lang.python > #11144

Re: how to dynamically generate __name__ for an object?

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 <ericsnowcurrently@gmail.com>
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 <afb6fca6-126a-44cf-8ef6-ba29c8f2ef93@r12g2000vbe.googlegroups.com>
References <mailman.2004.1312686417.1164.python-list@python.org> <afb6fca6-126a-44cf-8ef6-ba29c8f2ef93@r12g2000vbe.googlegroups.com>
Date Wed, 10 Aug 2011 10:54:54 -0600
Subject Re: how to dynamically generate __name__ for an object?
From Eric Snow <ericsnowcurrently@gmail.com>
To Fuzzyman <fuzzyman@gmail.com>
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 <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.2122.1312995302.1164.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Wed, Aug 10, 2011 at 8:48 AM, Fuzzyman <fuzzyman@gmail.com> wrote:
> On Aug 7, 4:06 am, Eric Snow <ericsnowcurren...@gmail.com> wrote:
>> Thought I knew how to provide a dynamic __name__ on instances of a
>> class.  My first try was to use a non-data descriptor:
>>
>> # module base.py
>>
>> class _NameProxy(object):
>>     def __init__(self, oldname):
>>         self.oldname = oldname
>>     def __get__(self, obj, cls):
>>         if obj is None:
>>             return self.oldname
>>         if "__name__" not in obj.__dict__:
>>             return str(obj.__context__)
>>         return obj.__name__
>>
>> class _BaseMeta(type):
>>     def __init__(cls, name, bases, namespace):
>>         cls.__name__ = _NameProxy(name)
>>
>> class Base(object):
>>     __metaclass__ = _BaseMeta
>>
>> $ python _base.py
>> Traceback (most recent call last):
>>   ...
>>   File "/usr/local/lib/python2.4/site-packages/base.py", line xx, in __init__
>>     cls.__name__ = _NameProxy(name)
>> TypeError: Error when calling the metaclass bases
>>     can only assign string to Base.__name__, not '_NameProxy'
>>
>> Needless to say I was surprised.  After looking in typeobject.c, I
>> believe that __name__ must be a string where classes are concerned[1].
>>  So 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?  Or 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):
> ...   @property
> ...   def __name__(self):
> ...     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!  <wink>

-eric

>
> Michael
> --
> http://voidspace.org.uk/
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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


Thread

how to dynamically generate __name__ for an object? Eric Snow <ericsnowcurrently@gmail.com> - 2011-08-06 21:06 -0600
  Re: how to dynamically generate __name__ for an object? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-07 14:47 +1000
    Re: how to dynamically generate __name__ for an object? Eric Snow <ericsnowcurrently@gmail.com> - 2011-08-07 02:05 -0600
  Re: how to dynamically generate __name__ for an object? Fuzzyman <fuzzyman@gmail.com> - 2011-08-10 07:48 -0700
    Re: how to dynamically generate __name__ for an object? Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-10 09:25 -0600
      Re: how to dynamically generate __name__ for an object? Fuzzyman <fuzzyman@gmail.com> - 2011-08-10 09:38 -0700
    Re: how to dynamically generate __name__ for an object? Eric Snow <ericsnowcurrently@gmail.com> - 2011-08-10 10:54 -0600

csiph-web