Python dynamic attribute creation
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Fri Jun 25 10:34:23 EDT 2010
WANG Cong a écrit :
> 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.
>
> My points are:
>
(snip)
Python's classes are plain objects, and like any other object are
created at runtime. Having to special-case them would break the
simplicity and uniformity of Python for no good reason. Just like
there's no good reason to make setattr() working differently for class
and non-class objects.
FWIW, what you call "metaprogramming" is just ordinary programming - at
least in Python. All your fears and concerns about Python's dynamism are
just a priori learned from the "mainstream" B&D culture. From
experience, these are non-issues - unless of course misused by some
fool, but then there's no way to prevent stupids from doing stupid
things. So, yes, Python rely quite a lot on programmer's common sense
and discipline. Now the good news is that is JustWork(tm).
More information about the Python-list
mailing list