473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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?

Aug 23 '05 #1
3 2048
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
Aug 23 '05 #2

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":)

Aug 23 '05 #3
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
2
by: lezah | last post by:
what is the difference to use the password file and normal account to login?
12
by: MikeT | last post by:
I have a page that produces little thumbnails of the 3D models it finds in a specified directory (and iterates down through any sub directories). It basically scans each directory for 3D Studio...
3
by: Razvan | last post by:
Hi! What is wrong with the following code ? // Test1.cpp : Defines the entry point for the console application. //
4
by: Andy Lomax | last post by:
Is there any difference between: foo A = B; and foo A; A = B; Specifically, I have a case where B is a function call which returns a
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
12
by: Eric Layman | last post by:
Hi, What's the difference between a normal web element: <input type="text" id="txtname" name="txtname" runat="server"> vs webcontrol text box: <asp:Textbox id="username" Columns="10"...
2
by: John Bailo | last post by:
What's the difference between #Eval and #Bind? I built a grid view control that used Bind for display. Then I wanted to format it conditionally -- so I converted it to DataBinder.Eval() It...
13
by: Hussein B | last post by:
Hi, I'm familiar with static method concept, but what is the class method? how it does differ from static method? when to use it? -- class M: def method(cls, x): pass method =...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.