473,804 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2075
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
6961
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 guess I'll try to narrow it down to a few specific questions, but any further input offered on the subject is greatly appreciated: 1. Are all of my class's methods supposed to take 'self' as their first arg? 2. Am I then supposed to call...
2
2047
by: lezah | last post by:
what is the difference to use the password file and normal account to login?
12
2050
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 Max files using the filesystemobject and writes an activeX component called iDrop for each file so it can be displayed on the page (and drag-dropped straight into Max). If it happens to find a similarly named XML file in the directory, it loads...
3
1830
by: Razvan | last post by:
Hi! What is wrong with the following code ? // Test1.cpp : Defines the entry point for the console application. //
4
1506
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
15666
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
12
3253
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" runat="server"> </asp:TextBox>
2
8511
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 still seemed to be able to do two-way binding ( updates, inserts ).
13
2748
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 = classmethod(method) --
44
2961
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 class is used as a function that keeps state from one call to the next. The problem is that I don't know what to call such a thing! "Abstract class" isn't right, because that implies that you should subclass the class and then instantiate the...
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10564
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.