473,473 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

About getattr()

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!
Feb 12 '07 #1
3 1531
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.
Feb 12 '07 #2
"Jm lists" <pr***********@gmail.comon 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
Feb 12 '07 #3
Leif K-Brooks <eu*****@ecritters.bizwrote:
>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')

Feb 12 '07 #4

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

Similar topics

2
by: Brian Roberts | last post by:
I'm confused about the use of hasattr/getattr, or possibly namespaces. I know how to do this: class UnderstandThis(object): def do_foo(self): pass def do_bar(self): pass def doit(self, cmd):...
3
by: Johnny | last post by:
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) Return the value of...
3
by: googleboy | last post by:
Hi there. I have defined a class called Item with several (about 30 I think) different attributes (is that the right word in this context?). An abbreviated example of the code for this is: ...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
13
by: Pierre | last post by:
Hi, Sorry in advance, english is not my main language :/ I'd like to customize the result obtained by getattr on an object : if the object has the requested property then return it BUT if the...
4
by: Hole | last post by:
Hi There! I'm trying to use Zope and the product OpenFlow. I got the following error while I was using the built-in function getattr() to retrieve an OpenFlow object: attribute name must be...
4
by: Emin | last post by:
Dear experts, I got some unexpected behavior in getattr and copy.deepcopy (see transcript below). I'm not sure if this is actually a bug in copy.deepcopy or if I'm doing something too magical...
1
by: Sergio Correia | last post by:
This works: # Module spam.py import eggs print getattr(eggs, 'omelet')(100) That is, I just call the function omelet inside the module eggs and evaulate it with the argument 100.
8
by: Gregor Horvath | last post by:
Hi, class A(object): test = "test" class B(object): a = A() In : B.a.test
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
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...
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.