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? 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
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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: lezah |
last post by:
what is the difference to use the password file and normal account to login?
|
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...
|
by: Razvan |
last post by:
Hi!
What is wrong with the following code ?
// Test1.cpp : Defines the entry point for the console application.
//
|
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
|
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...
|
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"...
|
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...
|
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 =...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |