Python dynamic attribute creation

Carl Banks pavlovevidence at gmail.com
Fri Jun 25 22:31:24 EDT 2010


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.

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.

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.


Carl Banks



More information about the Python-list mailing list