Variable interpolation question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Fabbro

    Variable interpolation question

    This is probably a beginner's question, but I'm stuck...please be kind
    to an ex-perler ;)

    How do I do something like this:

    for attr in dir(some_obj):
    if ( some_obj.attr == 0 ):
    print "Missing data: %s field %s" % ( some_obj.name,
    some_obj.attr)

    Of course, this gives
    "AttributeError : foo instance has no attribute 'attr'"

    I really don't want to use exec/eval, as that slows things down
    dramatically.

    Help?

    Thanks.

    -Drew
  • anton muhin

    #2
    Re: Variable interpolation question

    Andrew Fabbro wrote:
    [color=blue]
    > This is probably a beginner's question, but I'm stuck...please be kind
    > to an ex-perler ;)
    >
    > How do I do something like this:
    >
    > for attr in dir(some_obj):
    > if ( some_obj.attr == 0 ):
    > print "Missing data: %s field %s" % ( some_obj.name,
    > some_obj.attr)
    >
    > Of course, this gives
    > "AttributeError : foo instance has no attribute 'attr'"
    >
    > I really don't want to use exec/eval, as that slows things down
    > dramatically.
    >
    > Help?
    >
    > Thanks.
    >
    > -Drew[/color]

    You are probably looking for hasattr/getattr functions:

    for attr in dir(some_obj):
    if hasattr(some_ob j, attr) and getattr(some_ob j, attr) == 0:
    print 'blah...'

    of course, it could be shorter:

    .....
    if getattr(some_ob j, attr, 0) == 0:
    print 'blah'

    regards,
    anton.

    Comment

    • Bengt Richter

      #3
      Re: Variable interpolation question

      On Mon, 17 Nov 2003 20:47:51 +0300, anton muhin <antonmuhin.REM OVE.ME.FOR.REAL .MAIL@rambler.r u> wrote:
      [color=blue]
      >Andrew Fabbro wrote:
      >[color=green]
      >> This is probably a beginner's question, but I'm stuck...please be kind
      >> to an ex-perler ;)
      >>
      >> How do I do something like this:
      >>
      >> for attr in dir(some_obj):
      >> if ( some_obj.attr == 0 ):
      >> print "Missing data: %s field %s" % ( some_obj.name,
      >> some_obj.attr)
      >>
      >> Of course, this gives
      >> "AttributeError : foo instance has no attribute 'attr'"
      >>
      >> I really don't want to use exec/eval, as that slows things down
      >> dramatically.
      >>
      >> Help?
      >>
      >> Thanks.
      >>
      >> -Drew[/color]
      >
      >You are probably looking for hasattr/getattr functions:
      >
      >for attr in dir(some_obj):
      > if hasattr(some_ob j, attr) and getattr(some_ob j, attr) == 0:
      > print 'blah...'
      >
      >of course, it could be shorter:
      >
      >....
      > if getattr(some_ob j, attr, 0) == 0:
      > print 'blah'
      >[/color]
      Or why use an "== 0" test when just hasattr tests for presence/absence without using up
      a possible value of the attribute for flag purposes?

      BTW, The OP might want to note that in general

      for attr in dir(some_object ):
      if getattr(some_ob j, attr, 0) == 0:
      ...

      is not the same as (untested)

      objdict = vars(some_obj) # vars raises exception if there's no some_obj.__dict __
      for attr in objdict:
      if objdict[attr] == 0:
      ...

      or, safer, (untested)

      objdict = getattr(some_ob ject, '__dict__', {})
      for attr in objdict:
      if objdict[attr] == 0:
      ...

      or (untested)

      for attr in getattr(some_ob ject, '__dict__', {}): ...
      if some_obj.__dict __[attr] == 0: ...
      ...

      I.e., dir chases down all the inherited stuff, and getattr invokes all the magic
      associated with attribute access, such as properties, whereas just using
      some_obj.__dict __ bypasses that.

      Regards,
      Bengt Richter

      Comment

      • anton muhin

        #4
        Re: Variable interpolation question

        Bengt Richter wrote:[color=blue]
        > On Mon, 17 Nov 2003 20:47:51 +0300, anton muhin <antonmuhin.REM OVE.ME.FOR.REAL .MAIL@rambler.r u> wrote:
        >
        >[color=green]
        >>Andrew Fabbro wrote:
        >>
        >>[color=darkred]
        >>>This is probably a beginner's question, but I'm stuck...please be kind
        >>>to an ex-perler ;)
        >>>
        >>>How do I do something like this:
        >>>
        >>>for attr in dir(some_obj):
        >>> if ( some_obj.attr == 0 ):
        >>> print "Missing data: %s field %s" % ( some_obj.name,
        >>>some_obj.att r)
        >>>
        >>>Of course, this gives
        >>>"AttributeEr ror: foo instance has no attribute 'attr'"
        >>>
        >>>I really don't want to use exec/eval, as that slows things down
        >>>dramatically .
        >>>
        >>>Help?
        >>>
        >>>Thanks.
        >>>
        >>>-Drew[/color]
        >>
        >>You are probably looking for hasattr/getattr functions:
        >>
        >>for attr in dir(some_obj):
        >> if hasattr(some_ob j, attr) and getattr(some_ob j, attr) == 0:
        >> print 'blah...'
        >>
        >>of course, it could be shorter:
        >>
        >>....
        >> if getattr(some_ob j, attr, 0) == 0:
        >> print 'blah'
        >>[/color]
        >
        > Or why use an "== 0" test when just hasattr tests for presence/absence without using up
        > a possible value of the attribute for flag purposes?
        >[/color]
        [skipped][color=blue]
        > Regards,
        > Bengt Richter[/color]

        Of course you're right, I just forgot it and thought that OP wants to
        verify attribute's presence as well---mea culpa.

        regards,
        anton.

        Comment

        Working...