Python dynamic attribute creation

Stephen Hansen me+list/python at ixokai.io
Tue Jun 29 13:10:09 EDT 2010


On 6/29/10 9:48 AM, WANG Cong wrote:
> On 06/27/10 12:01, Carl Banks<pavlovevidence at gmail.com>  wrote:
>
>> On Jun 25, 8:24 pm, WANG Cong<xiyou.wangc... at gmail.com>  wrote:
>>> Understand, but please consider my proposal again, if we switched to:
>>>
>>> setattr(foo, 'new_attr', "blah")
>>>
>>> by default, isn't Python still dynamic as it is? (Please teach me if I
>>> am wrong here.)
>>>
>>> This why I said the questionable thing is not so much related with dynamic
>>> programming or not.
>>
>> Because it makes dynamicism harder to do.
>>
>> Like I said, Python's goal isn't simply to make dynamicism possible,
>> it's to make it easy.
>>
>> "foo.new_attr = 'blah'" is easier than using setattr.
>>
>
> I do agree it's easier, but why do we need this to be easy? This is
> really my question.

The question is fundamentally invalid, really.

"Why do we need this--"

What does need have to do with it?

We use the standard assignment and deletion semantics because there's no 
reason not to; the syntax is readable, understandable, straight-forward, 
and useful.

"Why not make it harder--" you've basically asked repeatedly.

Why would we make it harder?

There's no reason to.

There's no reason to change to "setattr by default", for although it 
would make Python dynamic, it would be harder to be dynamic. When given 
a choice between "easier" and "harder" the answer is simply, always, 
easier. It is not for us to defend *why* -- easier wins, period. If you 
think that harder would be better, it is for you to defend why it 
*needs* to be harder. Not vice versa.

"It is easier" is, by itself, the end of the justification. There need 
be no other, unless there is some compelling reason why making a certain 
thing easier is *bad* -- this is a real consideration that is made on 
occasion, but the burden of proof is on you.

> Also, since it is easier, why not drop the harder one, setattr()?

Why would we? There are some corner cases where setattr is easier then 
other options (such as thing.__dict__[key]).

There is no exclusive-OR here. There's no reason to drop "the harder one".

-- 

    ... 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