Python dynamic attribute creation

WANG Cong xiyou.wangcong at gmail.com
Sat Jun 26 00:22:36 EDT 2010


On 06/25/10 20:22, Ian Kelly <ian.g.kelly at gmail.com> wrote:

> On Fri, Jun 25, 2010 at 12:51 PM, Stephen Hansen
> <me+list/python at ixokai.io> wrote:
>>> Using assignments to create an attribute hides metaprogramming behide,
>>> while using delattr() exposes it.
>>
>> I don't understand what you're saying here either.
>
> I think he's saying that when an attribute exists in the class
> dictionary, assigning that attribute to an instance obscures it, and
> deleting that attribute from an instance exposes it.
>
> The point being, I guess, that when an assignment to an instance
> attribute is performed in the code, it's not immediately obvious
> whether that assignment is updating something already defined in the
> class or creating something entirely new.  Whereas deleting an
> instance attribute always exposes whatever is already defined at the
> class level.
>

Exactly. Sorry that I confused you.

> I think the distinction is false, though, since deleting an instance
> attribute says nothing about whether that attribute is defined at the
> class level to begin with.

As I replied in a previous email, this makes Python unify the operations
on class attribute with the operations on trivial dictionaries, yes,
this is good, but Python doesn't do enough, it doesn't go further to
allow dyname attribute _names_ which could make dyname class attribute
perfect. The PEP [1] is rejected for some reason that I don't know.

1. http://www.python.org/dev/peps/pep-0363/

Even if it did accept this, I still hold my points on whether this
should be done by assignments by default.

Thanks.


-- 
Live like a child, think like the god.
 



More information about the Python-list mailing list