Re: Python dynamic attribute creation

Discussion in 'Python' started by Bruno Desthuilliers, Jun 25, 2010.

  1. 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).
    Bruno Desthuilliers, Jun 25, 2010
    #1
    1. Advertising

  2. Bruno Desthuilliers

    Ethan Furman Guest

    WANG Cong wrote:
    > On 06/25/10 15:34, Bruno Desthuilliers <> wrote:
    >
    >> 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.
    >>

    >
    > For implementaiton, perhaps, but not for the language design, how could
    > a language design be perfect if we can use setattr() like assignments
    > while use other things, e.g. delattr(), not? Is there any way to express
    > delattr() as simple as expressing setattr() with assignments? I doubt...


    >>> del test.a
    >>> test.a

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: class test has no attribute 'a'

    Looks pretty simple to me...

    ~Ethan~
    Ethan Furman, Jun 25, 2010
    #2
    1. Advertising

  3. WANG Cong a écrit :
    > On 06/25/10 15:34, Bruno Desthuilliers <> wrote:
    >
    >> 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.
    >>

    >
    > For implementaiton, perhaps, but not for the language design, how could
    > a language design be perfect if we can use setattr() like assignments
    > while use other things, e.g. delattr(), not? Is there any way to express
    > delattr() as simple as expressing setattr() with assignments? I doubt...


    cf Ethan's answer on this.

    > Using assignments to create an attribute hides metaprogramming
    > while using delattr() exposes it.


    Once again : in Python, none of this is "metaprogramming" - just plain
    ordinary programming. So called "metaprogramming" is just an artefact of
    static languages where datastructures are created at compile time.
    Bruno Desthuilliers, Jun 27, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Warren
    Replies:
    5
    Views:
    456
    Russell Warren
    Jan 17, 2006
  2. Steven D'Aprano

    Re: Python dynamic attribute creation

    Steven D'Aprano, Jun 25, 2010, in forum: Python
    Replies:
    15
    Views:
    418
    Stephen Hansen
    Jul 1, 2010
  3. Neil Hodgson

    Re: Python dynamic attribute creation

    Neil Hodgson, Jun 26, 2010, in forum: Python
    Replies:
    12
    Views:
    448
  4. Carl Banks

    Re: Python dynamic attribute creation

    Carl Banks, Jun 26, 2010, in forum: Python
    Replies:
    26
    Views:
    837
    Bruno Desthuilliers
    Jul 2, 2010
  5. Andre Alexander Bell

    Re: Python dynamic attribute creation

    Andre Alexander Bell, Jun 29, 2010, in forum: Python
    Replies:
    8
    Views:
    860
    John Nagle
    Jul 9, 2010
Loading...

Share This Page