About getattr()

Discussion in 'Python' started by Jm lists, Feb 12, 2007.

  1. Jm lists

    Jm lists Guest

    Hello,

    Since I can write the statement like:

    >>> print os.path.isdir.__doc__

    Test whether a path is a directory

    Why do I still need the getattr() func as below?

    >>> print getattr(os.path,"isdir").__doc__

    Test whether a path is a directory

    Thanks!
     
    Jm lists, Feb 12, 2007
    #1
    1. Advertising

  2. Jm lists wrote:
    > Since I can write the statement like:
    >
    >>>> print os.path.isdir.__doc__

    > Test whether a path is a directory
    >
    > Why do I still need the getattr() func as below?
    >
    >>>> print getattr(os.path,"isdir").__doc__

    > Test whether a path is a directory


    You don't. getattr() is only useful when the attribute name is
    determined at runtime.
     
    Leif K-Brooks, Feb 12, 2007
    #2
    1. Advertising

  3. "Jm lists" <> on Mon, 12 Feb 2007 12:36:10
    +0800 didst step forth and proclaim thus:

    > Hello,
    >
    > Since I can write the statement like:
    >
    > >>> print os.path.isdir.__doc__

    > Test whether a path is a directory
    >
    > Why do I still need the getattr() func as below?
    >
    > >>> print getattr(os.path,"isdir").__doc__

    > Test whether a path is a directory


    getattr lets you lookup an attribute given a string, so the attribute
    wouldn't have to be hardcoded in your program, it could come from a
    file, or from user input.

    --
    Sam Peterson
    skpeterson At nospam ucdavis.edu
    "if programmers were paid to remove code instead of adding it,
    software would be much better" -- unknown
     
    Samuel Karl Peterson, Feb 12, 2007
    #3
  4. Jm lists

    Duncan Booth Guest

    Leif K-Brooks <> wrote:

    >> Why do I still need the getattr() func as below?
    >>
    >>>>> print getattr(os.path,"isdir").__doc__

    >> Test whether a path is a directory

    >
    > You don't.


    Correct

    > getattr() is only useful when the attribute name is
    > determined at runtime.
    >

    getattr() is useful in at least 2 other situations:

    maybe you know the attribute name in advance, but don't know whether the
    object has that particular attribute, then the 3 argument form of getattr
    could be appropriate:

    print getattr(x, 'attr', 'some default')

    or if the attribute name isn't a valid Python identifier:

    print getattr(x, 'class')
     
    Duncan Booth, Feb 12, 2007
    #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. daishi
    Replies:
    0
    Views:
    558
    daishi
    Jul 25, 2003
  2. Srikanth Mandava

    getattr

    Srikanth Mandava, Feb 19, 2004, in forum: Python
    Replies:
    2
    Views:
    459
    Peter Hansen
    Feb 19, 2004
  3. Brian Roberts

    Confused about hasattr/getattr/namespaces

    Brian Roberts, Feb 29, 2004, in forum: Python
    Replies:
    2
    Views:
    307
    Bob Ippolito
    Feb 29, 2004
  4. SimonVC

    getattr() in default namespace.

    SimonVC, Apr 8, 2004, in forum: Python
    Replies:
    3
    Views:
    902
    SimonVC
    Apr 9, 2004
  5. Thomas Rast

    getattr() woes

    Thomas Rast, Dec 29, 2004, in forum: Python
    Replies:
    4
    Views:
    633
    Kamilche
    Dec 31, 2004
Loading...

Share This Page