Python dynamic attribute creation
Alexander Kapps
alex.kapps at web.de
Sat Jun 26 17:59:02 EDT 2010
Stephen Hansen wrote:
> 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.
Well, I would sympathize with an attempt to add some kind of typo
protection. Not build into the language, but I dream about a command
line switch that invokes a test like pylint/PyChecker. I wish any of
those would enter the standard distribution.
> 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".
I fully agree with everything so far. As I said, i don't support
this proposal, just that I can see a reason why some people might
want this. I do this type of dynamic attribute addition (both data
and methods) all day in my interactive sessions, which are a large
part of my Python activity.
> 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.
That was an interesting insight, thank you for this. While I
actually know all this, I indeed still seem to sometimes treat
objects as "fixed-once-defined-and-created" entities. I just grep'ed
all my code repository and found that I almost never do any
on-the-fly attribute addition (totally contrary to my interactive work)
You may have just opened the door the my next level of Python OO
understanding. Thank you! :-)
> 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.
More information about the Python-list
mailing list