What's the difference between built-in func getattr() and normal call of a func of a class

Discussion in 'Python' started by Johnny, Aug 23, 2005.

  1. Johnny

    Johnny Guest

    Hi,

    I wonder what is the difference between the built-in function
    getattr() and the normal call of a function of a class. Here is the
    details:

    getattr( object, name[, default])

    Return the value of the named attributed of object. name must be a
    string. If the string is the name of one of the object's attributes,
    the result is the value of that attribute. For example, getattr(x,
    'foobar') is equivalent to x.foobar. If the named attribute does not
    exist, default is returned if provided, otherwise AttributeError is
    raised.

    Is that to say the only difference between the two is that no
    matter the specific function exists or not the built-in func will
    always return a value, but "class.function" will not?
    Johnny, Aug 23, 2005
    #1
    1. Advertising

  2. Re: What's the difference between built-in func getattr() and normalcall of a func of a class

    Johnny wrote:
    > Hi,
    >
    > I wonder what is the difference between the built-in function
    > getattr() and the normal call of a function of a class. Here is the
    > details:
    >
    > getattr( object, name[, default])
    >
    > Return the value of the named attributed of object. name must be a
    > string. If the string is the name of one of the object's attributes,
    > the result is the value of that attribute. For example, getattr(x,
    > 'foobar') is equivalent to x.foobar. If the named attribute does not
    > exist, default is returned if provided, otherwise AttributeError is
    > raised.
    >
    > Is that to say the only difference between the two is that no
    > matter the specific function exists or not the built-in func will
    > always return a value, but "class.function" will not?


    No, it will only return _always_ a value if you provide a default one.
    If not, they have the exact same semantics.

    What you've got here is something usually called "syntactic sugaring" -
    a specialized syntax that performs certain instructions that _could_ be
    done by hand - but the concise syntax is (supposedly, and certainly in
    this case) easier to read/write/understand.

    There are others - e.g. list comprehensions or a < b < c.

    Regards,

    Diez
    Diez B. Roggisch, Aug 23, 2005
    #2
    1. Advertising

  3. Johnny

    Johnny Guest

    Diez B. Roggisch wrote:
    > No, it will only return _always_ a value if you provide a default one.
    > If not, they have the exact same semantics.
    >
    > What you've got here is something usually called "syntactic sugaring" -
    > a specialized syntax that performs certain instructions that _could_ be
    > done by hand - but the concise syntax is (supposedly, and certainly in
    > this case) easier to read/write/understand.
    >
    > There are others - e.g. list comprehensions or a < b < c.
    >
    > Regards,
    >
    > Diez


    Cool~
    Thanks for showing me the "syntactic sugaring":)
    Johnny, Aug 23, 2005
    #3
  4. Johnny

    Robert Kern Guest

    Re: What's the difference between built-in func getattr() and normalcall of a func of a class

    Johnny wrote:
    > Hi,
    >
    > I wonder what is the difference between the built-in function
    > getattr() and the normal call of a function of a class. Here is the
    > details:
    >
    > getattr( object, name[, default])
    >
    > Return the value of the named attributed of object. name must be a
    > string. If the string is the name of one of the object's attributes,
    > the result is the value of that attribute. For example, getattr(x,
    > 'foobar') is equivalent to x.foobar. If the named attribute does not
    > exist, default is returned if provided, otherwise AttributeError is
    > raised.
    >
    > Is that to say the only difference between the two is that no
    > matter the specific function exists or not the built-in func will
    > always return a value, but "class.function" will not?


    No, getattr(object, name) will raise an AttributeError if the attribute
    doesn't exist; no value will be returned. When getattr(object, name,
    default) is called, then a value will be returned regardless of whether
    or not the attribute exists (but possibly something else may go wrong).

    In terms of practice, the getattr() form allows you to specify the
    attribute at run-time rather than write-time. You can do things like
    iterate over a list of attributes.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Aug 23, 2005
    #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. jakk
    Replies:
    4
    Views:
    12,118
  2. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    Difference between casting by as and normal cast

    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Aug 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    409
    Brock Allen
    Aug 15, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    501
    Hari Sekhon
    Jun 20, 2006
  4. Masudur
    Replies:
    12
    Views:
    1,025
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 26, 2007
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    186
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page