Note about getattr and '.'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • szport@gmail.com

    #1

    Note about getattr and '.'

    There is an interesting skewness in python:

    class A(object): pass
    >>a=A()
    >>setattr(a, '$foo', 17)
    >>getattr(a, '$foo')
    17

    But I can't write
    >>a.'$foo'
  • Mathias Panzenboeck

    #2
    Re: Note about getattr and '.'

    szport@gmail.co m wrote:
    There is an interesting skewness in python:
    >
    class A(object): pass
    >
    >>>a=A()
    >>>setattr(a, '$foo', 17)
    >>>getattr(a, '$foo')
    17
    >
    But I can't write
    >>>a.'$foo'
    >
    Yes, this is known. I think IronPython uses a specialized dictionary for members, which prohibits
    malformed names. I don't know if there will be such a dictionary in any future CPython version.
    (Would be good.)

    panzi

    Comment

    • Carl Banks

      #3
      Re: Note about getattr and '.'


      szport@gmail.co m wrote:
      There is an interesting skewness in python:
      >
      class A(object): pass
      >
      >a=A()
      >setattr(a, '$foo', 17)
      >getattr(a, '$foo')
      17
      >
      But I can't write
      >a.'$foo'
      Likewise, you can write

      setattr(a,'hell o',17)

      but you can't write

      a.'hello'

      but you can write

      a.hello

      The only thing that can follow the "." (attribute access) operator is a
      valid Python identifier. Strings may not. Anyways, it's probably not
      a good idea in most cases to use getattr and setattr with a
      non-identifier, but Python doesn't prevent you from doing it. (Which
      is good in the occasional case where it is a good idea.)

      BTW, Matlab has an interesting syntax; you could have written this:

      a.("$foo")

      (And because Matlab is weird that way, this was not structure member
      access, but some weird hybrid of structure accces and indexing and/or
      slicing. The syntax was very useful in Matlab because there's no
      built-in associative array and using this syntax was the easiest way to
      get something like it. In Python we'd do that with a dict, so it's not
      so useful here.)


      Carl Banks

      Comment

      • Jonathan Ballet

        #4
        Re: Note about getattr and '.'

        Le 21 Nov 2006 13:21:52 -0800,
        szport@gmail.co m a écrit :
        There is an interesting skewness in python:

        class A(object): pass
        >a=A()
        >setattr(a, '$foo', 17)
        >getattr(a, '$foo')
        17

        But I can't write
        >a.'$foo'
        Well, you can even do :
        >>class A(object): pass
        ....
        >>a = A()
        >>setattr(a, 'some.attr', 15)
        >>a.some.attr
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        AttributeError: 'A' object has no attribute 'some'
        >>getattr(a, 'some.attr')
        15
        >>>
        Which is a lot funnier IMHO :)

        - Jon

        Comment

        • Steven D'Aprano

          #5
          Re: Note about getattr and '.'

          On Tue, 21 Nov 2006 22:39:09 +0100, Mathias Panzenboeck wrote:
          szport@gmail.co m wrote:
          >There is an interesting skewness in python:
          >>
          >class A(object): pass
          >>
          >>>>a=A()
          >>>>setattr(a , '$foo', 17)
          >>>>getattr(a , '$foo')
          >17
          >>
          >But I can't write
          >>>>a.'$foo'
          >>
          >
          Yes, this is known. I think IronPython uses a specialized dictionary for members, which prohibits
          malformed names. I don't know if there will be such a dictionary in any future CPython version.
          (Would be good.)

          Why would it be good?

          How many bugs have you found that were caused by this behaviour?



          --
          Steven D'Aprano

          Comment

          • Carl Banks

            #6
            Re: Note about getattr and '.'


            Steven D'Aprano wrote:
            On Tue, 21 Nov 2006 22:39:09 +0100, Mathias Panzenboeck wrote:
            Yes, this is known. I think IronPython uses a specialized dictionary for members, which prohibits
            malformed names. I don't know if there will be such a dictionary in any future CPython version.
            (Would be good.)
            >
            Why would it be good?
            >
            How many bugs have you found that were caused by this behaviour?
            It's not bugs. A specialized dictionary could be better optimized if
            you know it can only hold Python identifiers. There's talk of such a
            dictionary in Python 3000.


            Carl Banks

            Comment

            Working...