Interesting things of 'getattr' and 'setattr'

Discussion in 'Python' started by Red Forks, Dec 15, 2009.

  1. Red Forks

    Red Forks Guest

    I don't know it is a feature, or implement detail:

    >>> class C(object): pass

    ....
    >>> c = C()
    >>> setattr(c, ' ', 3)
    >>> getattr(c, ' ')

    3
    >>> setattr(c, 'with blank', 4)
    >>> getattr(c, 'with blank')

    4

    getattr / setattr seems treat any string as attribute name.
    Red Forks, Dec 15, 2009
    #1
    1. Advertising

  2. Red Forks, 15.12.2009 13:19:
    > I don't know it is a feature, or implement detail:
    >
    > >>> class C(object): pass

    > ...
    > >>> c = C()
    > >>> setattr(c, ' ', 3)
    > >>> getattr(c, ' ')

    > 3
    > >>> setattr(c, 'with blank', 4)
    > >>> getattr(c, 'with blank')

    > 4
    >
    > getattr / setattr seems treat any string as attribute name.


    Feature. We're all adults.

    Stefan
    Stefan Behnel, Dec 15, 2009
    #2
    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. Paulo da Silva

    getattr/setattr q.

    Paulo da Silva, Apr 3, 2007, in forum: Python
    Replies:
    10
    Views:
    549
    Paulo da Silva
    Apr 3, 2007
  2. Nathan Harmston

    recursion error using setattr and getattr

    Nathan Harmston, Jun 7, 2007, in forum: Python
    Replies:
    0
    Views:
    286
    Nathan Harmston
    Jun 7, 2007
  3. Simon Brunning
    Replies:
    0
    Views:
    498
    Simon Brunning
    Jun 7, 2007
  4. Donn Ingle

    setattr getattr confusion

    Donn Ingle, Dec 8, 2007, in forum: Python
    Replies:
    7
    Views:
    291
    Donn Ingle
    Dec 8, 2007
  5. maestro

    setattr and getattr, when to use?

    maestro, Aug 23, 2008, in forum: Python
    Replies:
    4
    Views:
    338
    Bruno Desthuilliers
    Aug 26, 2008
Loading...

Share This Page