Python dynamic attribute creation

Ixokai ixokai at ixokai.io
Fri Jun 25 23:30:15 EDT 2010


On Fri, Jun 25, 2010 at 8:24 PM, WANG Cong <xiyou.wangcong at gmail.com> wrote:

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

In what possible way is:

    setattr(foo, 'new_attr', 'blah')
    getattr(foo, 'new_attr')
    delattr(foo, 'new_attr')

Better then:

    foo.new_attr = 'blah'
    foo.new_attr
    del foo.new_attr

I don't understand what your argument is or problem is with the regular
syntax, if you want to allow the former (all of which is currently possible
in Python if you prefer this style) but not the latter (all of which also
works, it just uses normal syntax as everyone would expect).

Do you think it should be somehow "tricky" or "more difficult" to
dynamically modify an instance at runtime? For that to hold, you have to
provide some pretty compelling reasoning why dynamically modifying an
instance at runtime is Not Good. Only then is there a good reason to make it
more difficult. (Since Python doesn't really restrict things, just makes
certain rare things that are undesirable a little harder to do)

--Stephen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100625/5b5bec4a/attachment-0001.html>


More information about the Python-list mailing list