Variable interpolation question

Discussion in 'Python' started by Andrew Fabbro, Nov 17, 2003.

  1. 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
    Andrew Fabbro, Nov 17, 2003
    #1
    1. Advertising

  2. Andrew Fabbro

    anton muhin Guest

    Andrew Fabbro wrote:

    > 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


    You are probably looking for hasattr/getattr functions:

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

    of course, it could be shorter:

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

    regards,
    anton.
    anton muhin, Nov 17, 2003
    #2
    1. Advertising

  3. On Mon, 17 Nov 2003 20:47:51 +0300, anton muhin <> wrote:

    >Andrew Fabbro wrote:
    >
    >> 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

    >
    >You are probably looking for hasattr/getattr functions:
    >
    >for attr in dir(some_obj):
    > if hasattr(some_obj, attr) and getattr(some_obj, attr) == 0:
    > print 'blah...'
    >
    >of course, it could be shorter:
    >
    >....
    > if getattr(some_obj, attr, 0) == 0:
    > print 'blah'
    >

    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_obj, 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_object, '__dict__', {})
    for attr in objdict:
    if objdict[attr] == 0:
    ...

    or (untested)

    for attr in getattr(some_object, '__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
    Bengt Richter, Nov 17, 2003
    #3
  4. Andrew Fabbro

    anton muhin Guest

    Bengt Richter wrote:
    > On Mon, 17 Nov 2003 20:47:51 +0300, anton muhin <> wrote:
    >
    >
    >>Andrew Fabbro wrote:
    >>
    >>
    >>>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

    >>
    >>You are probably looking for hasattr/getattr functions:
    >>
    >>for attr in dir(some_obj):
    >> if hasattr(some_obj, attr) and getattr(some_obj, attr) == 0:
    >> print 'blah...'
    >>
    >>of course, it could be shorter:
    >>
    >>....
    >> if getattr(some_obj, attr, 0) == 0:
    >> print 'blah'
    >>

    >
    > 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?
    >

    [skipped]
    > Regards,
    > Bengt Richter


    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.
    anton muhin, Nov 17, 2003
    #4
    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. \Rob\
    Replies:
    8
    Views:
    613
    Daniel Kirsch
    Feb 24, 2006
  2. Oliver Heins
    Replies:
    2
    Views:
    115
    Oliver Heins
    Sep 11, 2006
  3. Praveen Kallakuri
    Replies:
    3
    Views:
    155
    Praveen Kallakuri
    Apr 15, 2004
  4. phrankster

    Variable Interpolation with %%variable

    phrankster, Aug 9, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    132
    James Taylor
    Aug 14, 2005
  5. ccc31807

    double variable interpolation question

    ccc31807, May 22, 2009, in forum: Perl Misc
    Replies:
    8
    Views:
    94
    Mart van de Wege
    May 22, 2009
Loading...

Share This Page