how to dynamically generate __name__ for an object?

Eric Snow ericsnowcurrently at gmail.com
Sun Aug 7 10:05:46 CEST 2011


On Sat, Aug 6, 2011 at 10:47 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Eric Snow 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:
>
> Perhaps you should explain what you are trying to do. If you want to give
> instances their own name, why not just give them an instance
> attribute "name"? Double-leading-and-trailing-underscore attributes are
> reserved for Python, and have special semantics.

Again useful information failed to make it from my head to the
keyboard. :)  Currently I am looking at objects, regardless of their
types, and keying off their name.  If it is a module, class, or
function object, the name should be stored in __name__ already.  If it
is an instance of the Base class I demonstrated, I would like to
maintain that convention.

However, you may have a good point.  In other code I can simply check
for the name attribute I choose for my objects and also check for
__name__.  I suppose I just liked the idea of having a single
attribute to check.

-eric

>
>
> --
> Steven
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list