Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10998
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'assign': 0.04; 'instance,': 0.05; 'subject:object': 0.07; 'python': 0.08; '__name__': 0.09; 'dynamically': 0.09; 'none:': 0.09; 'def': 0.15; 'skip:" 40': 0.15; '__init__': 0.16; 'attribute,': 0.16; 'bases,': 0.16; 'metaclass': 0.16; 'needless': 0.16; 'non-data': 0.16; 'obj,': 0.16; 'url:hg': 0.16; 'to:name:python-list': 0.18; '(most': 0.21; 'maybe': 0.21; 'traceback': 0.24; 'string': 0.26; 'classes': 0.28; 'message-id:@mail.gmail.com': 0.29; 'module': 0.30; 'url:default': 0.30; 'typeerror:': 0.30; 'class': 0.30; 'subject:?': 0.31; 'error': 0.32; "isn't": 0.33; 'to:addr:python- list': 0.33; 'calling': 0.33; '...': 0.34; 'last):': 0.34; 'file': 0.36; 'url:python': 0.36; 'class.': 0.37; 'something': 0.37; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'provided': 0.60; 'believe': 0.65; 'received:209.85.215.174': 0.67; 'received:mail-ey0-f174.google.com': 0.67 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=wrwpvrJNMy7W/4x0EloGrC0yQOsluJRQJT4KNSd59vM=; b=NKogMZcqPYpkdDX02ZBj6/BH8BJP21O3zdO9iITg26SPxZQKeLMwQ1xGCGFIak8EVs 3+kgFayv9IaD6nEFUJSIvNEopDHq8ilONFEtM/N97b6hrgaj37LeNWVOnOGZ22YTIusB Z4wV5SN8MaN2XbhDQt9PiqwaqQHbss5nISylI= |
| MIME-Version | 1.0 |
| Date | Sat, 6 Aug 2011 21:06:51 -0600 |
| Subject | how to dynamically generate __name__ for an object? |
| From | Eric Snow <ericsnowcurrently@gmail.com> |
| To | python-list <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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.2004.1312686417.1164.python-list@python.org> (permalink) |
| Lines | 43 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1312686417 news.xs4all.nl 23873 [2001:888:2000:d::a6]:34671 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:10998 |
Show key headers only | View raw
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?
-eric
[1] http://hg.python.org/cpython/file/default/Objects/typeobject.c#l244
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll 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