setattr using invalid attribute names - bug or feature?

Discussion in 'Python' started by Gerson Kurz, Jun 14, 2004.

  1. Gerson Kurz

    Gerson Kurz Guest

    I stumbled across this (while using my homebrewn enum class):

    class test:
    pass

    instance = test()
    setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)

    I would've expected some kind of error message here when calling
    setattr(); after all, its not a regular attribute? Plus, documentation
    says

    "
    Set a named attribute on an object; setattr(x, 'y', v) is equivalent
    to
    ``x.y = v''.
    "

    and you cannot write this:

    instance.THIS :*2+~# IS OBVIOUSLY INVALID = 123

    (oh, and its: PythonWin 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200
    32 bit (Intel)] on win32.)
    Gerson Kurz, Jun 14, 2004
    #1
    1. Advertising

  2. Gerson Kurz

    Peter Hansen Guest

    Gerson Kurz wrote:

    > I stumbled across this (while using my homebrewn enum class):
    >
    > class test:
    > pass
    >
    > instance = test()
    > setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)
    >
    > I would've expected some kind of error message here when calling
    > setattr(); after all, its not a regular attribute?


    Okay. But so what?

    -Peter
    Peter Hansen, Jun 14, 2004
    #2
    1. Advertising

  3. Gerson Kurz

    Yermat Guest

    Peter Hansen wrote:
    > Gerson Kurz wrote:
    >
    >> I stumbled across this (while using my homebrewn enum class):
    >>
    >> class test:
    >> pass
    >>
    >> instance = test()
    >> setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)
    >>
    >> I would've expected some kind of error message here when calling
    >> setattr(); after all, its not a regular attribute?

    >
    >
    > Okay. But so what?
    >
    > -Peter


    And sometime it is usefull to create some attributes that can unlikely
    be used by the programmer (for example for cache or...).

    I've seen code on coockbook that were using that property.

    --
    Yermat
    Yermat, Jun 14, 2004
    #3
  4. Gerson Kurz

    Meno Guest

    (Gerson Kurz) wrote in message news:<-online.de>...
    > I stumbled across this (while using my homebrewn enum class):
    >
    > class test:
    > pass
    >
    > instance = test()
    > setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)
    >
    > I would've expected some kind of error message here when calling
    > setattr(); after all, its not a regular attribute? Plus, documentation
    > says
    >
    > "
    > Set a named attribute on an object; setattr(x, 'y', v) is equivalent
    > to
    > ``x.y = v''.
    > "
    >
    > and you cannot write this:
    >
    > instance.THIS :*2+~# IS OBVIOUSLY INVALID = 123


    No, but you can write this:

    >>> a = getattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID")
    >>> print a

    123

    Meno.
    Meno, Jun 14, 2004
    #4
  5. Gerson Kurz

    Larry Bates Guest

    You can also do this:

    class test:
    pass


    instance = test()
    setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)

    >>> print instance.__dict__["THIS :*2+~# IS OBVIOUSLY INVALID"]
    >>> 123


    >>> print instance.__dict__.keys()
    >>> ['THIS :*2+~# IS OBVIOUSLY INVALID']


    >>> print instance.__dict__.items()
    >>> [('THIS :*2+~# IS OBVIOUSLY INVALID', 123)]


    The only reason that you cannot do:

    instance.This :*2+~# IS OBVIOUSLY INVALID

    is because attribute names accessed in this
    fashion can't have spaces.

    Larry Bates,
    Syscon, Inc.

    "Meno" <> wrote in message
    news:...
    > (Gerson Kurz) wrote in message

    news:<-online.de>...
    > > I stumbled across this (while using my homebrewn enum class):
    > >
    > > class test:
    > > pass
    > >
    > > instance = test()
    > > setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)
    > >
    > > I would've expected some kind of error message here when calling
    > > setattr(); after all, its not a regular attribute? Plus, documentation
    > > says
    > >
    > > "
    > > Set a named attribute on an object; setattr(x, 'y', v) is equivalent
    > > to
    > > ``x.y = v''.
    > > "
    > >
    > > and you cannot write this:
    > >
    > > instance.THIS :*2+~# IS OBVIOUSLY INVALID = 123

    >
    > No, but you can write this:
    >
    > >>> a = getattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID")
    > >>> print a

    > 123
    >
    > Meno.
    Larry Bates, Jun 14, 2004
    #5
    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. Carl
    Replies:
    0
    Views:
    521
  2. dataangel

    Using pickle with setattr

    dataangel, Oct 13, 2004, in forum: Python
    Replies:
    0
    Views:
    264
    dataangel
    Oct 13, 2004
  3. dataangel

    Re: Using pickle with setattr

    dataangel, Oct 14, 2004, in forum: Python
    Replies:
    3
    Views:
    312
    dataangel
    Oct 15, 2004
  4. Alex
    Replies:
    3
    Views:
    286
    Bengt Richter
    Nov 21, 2005
  5. 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
Loading...

Share This Page