Python dynamic attribute creation

WANG Cong xiyou.wangcong at gmail.com
Fri Jun 25 23:24:10 EDT 2010


On 06/26/10 03:31, Carl Banks <pavlovevidence at gmail.com> wrote:

> On Jun 25, 6:15 am, WANG Cong <xiyou.wangc... at gmail.com> wrote:
>> Hi, list!
>>
>> I have a doubt about the design of dynamic attribute creation by
>> assignments in Python.
>>
>> As we know, in Python, we are able to create a new attribute of
>> a class dynamically by an assignment:
>>
>>
>>
>> >>> class test: pass
>> ...
>> >>> test.a = "hello"
>> >>> test.a
>> 'hello'
>>
>> However, I still don't get the points why Python designs it like this.
> [snip points]
>
>
> I understand you and think your proposal is reasonable.  I know a few
> people who use Python who would agree with you, and even go farther.
>
> You did well to propose not banning attributes per se, but making it
> harder to set them.  Python isn't really in the business of making
> questionable things impossible (there are occasions where it does,
> like killing threads), but it is in the business of making things that
> are questionable enough harder.  What you propose is reasonable under
> Python's design principles (provided that initializer functions were
> exempted), and if Python had been designed as you propose, I don't
> think it would have suffered that much.
>

Thanks much for encouraging me! This makes me feeling less guilty. ;-)


> Here's the thing: Python doesn't consider creating dynamic attributes
> to be questionable.  Python doesn't merely allow for dynamicism, it
> encourages it.  And encouraging something means to make it simple.
>

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.

> I know you won't agree with this, and I'm not trying to convince you
> or argue with you.  I'm just telling you why it's like that.
>
> I will also tell you that claims like "it's doesn't use good OOP
> principles" or "C++ does it that way" carry almost no credit.  How C++
> does something is a suggestion, nothing more.
>

In fact I wanted to avoid comparing Python with C++. :) When I talked
about OOP, it is general OOP, not related with any concrete programming languages.

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



More information about the Python-list mailing list