Python dynamic attribute creation

Stephen Hansen me+list/python at ixokai.io
Sat Jun 26 13:10:39 EDT 2010


On 6/26/10 9:01 AM, Alexander Kapps wrote:
> While I personally don't agree with this proposal (but I understand why
> some people might want it), I can see a reason.
>
> When disallowing direct attribute creation, those typos that seem to
> catch newcommers won't happen anymore. What I mean is this:

I get where you're coming from, but I don't see why "attributes" should 
get such special typo-protection when locals and any other names shouldn't.

I see it as an extension of "Special cases aren't special enough to 
break the rules." -- and here I think the OP and I disagree most strongly.

This characterization of adding attributes to an object as something 
"else", some special kind of activity called "metaprogramming" I think I 
reject outright, whereas I believe -- though I do not claim to speak for 
him/her -- the OP's position is that using 'syntax' to add attributes is 
actually a special case/activity.

I consider it the standard, normal behavior.

If this is how I create a local variable:
     x = 1

And to read that variable, I simply refer to it as "x", and if to read a 
defined attribute from an object I do:
     a.x

Then, to me, the way in which I would set a new variable on that object 
is clearly:
     a.x = 1

I don't see why Python should special case setting an attribute on an 
object to be so wildly different then setting a local variable (or 
global, or anything else) as to require a special function call. The 
activity of "adding an attribute to an object" is no more special, IMHO, 
then "adding a variable to the local scope".

Now, true: I fully acknowledge that if you're in an OOP-mindset and 
you're choosing to use a certain style of programming (and one I 
frequently indulge in), then you may /choose/ to treat certain objects 
as special, as being more firmly structured, as having a formal definition.

In that situation, certainly: adding an attribute on the fly to that 
formal definition seems entirely strange and special of an activity. But 
that's only because you *chose* to *see* and *use* the object that way. 
The "special"ness of the activity is entirely in your head, and to 
Python, its an entirely normal event.

Python lets you associate significance with normal events so you can get 
things done. But its just plodding along not really drinking whatever 
kool-aid you are, though its happy you like your flavor and is entirely 
content with letting you think its playing ball with you on that.


-- 

    ... Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/




More information about the Python-list mailing list