471,354 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Help needed in understanding the "getattr()" function

numberwhun
3,503 Expert Mod 2GB
Hello everyone! I am presently going through the "Dive Into Python" tutorial which I obtained from their website. No, this is not for any class, I am self-learning the Python language.

I am in Chapter 4 and reading about "Getting Object References with getattr". First, it say in there that, "you can get a reference to a function without knowing its name until run−time, by using the getattr function". That is the first thing that is a little confusing as it doesn't really elaborate on that too much, that I can tell. What does that mean? I know that you can do:

Expand|Select|Wrap|Line Numbers
  1. getattr({}, "clear)
  2.  
and that will tell you that it is the clear function of a dictionary. But, what does that have to do with not knowing the function name till runtime?

Next, if you look at example 4.10, there is a line of code in there as follows:

Expand|Select|Wrap|Line Numbers
  1. getattr(li, "append")("Moe")
  2.  
and in the text below the examples section it says:

Expand|Select|Wrap|Line Numbers
  1. In case it hasn't sunk in just how incredibly useful this is, try this: the return value of getattr is the method, which you can then call just as if you had said li.append("Moe") directly. But you didn't call the function directly; you specified the function name as a string instead.
  2.  
To tell you the truth, it hasn't really sunk in, even yet. First, it says the return value is the method (append) and that you can then call it as if you had done an:

Expand|Select|Wrap|Line Numbers
  1. li.append("Moe")
  2.  
Why not just do the direct call? Why go through all the trouble of the getattr() function?

I guess what I am getting at is, what is the getattr() function really useful for, because right now, I just don't see it? Can someone please explain it in a way that will allow me to see its full usefulness?

Sorry for the ignorance if this tends to be a clouded view!

Regards and Happy New Year!

Jeff
Dec 31 '07 #1
3 2895
Motoma
3,237 Expert 2GB
Hello everyone! I am presently going through the "Dive Into Python" tutorial which I obtained from their website. No, this is not for any class, I am self-learning the Python language.

I am in Chapter 4 and reading about "Getting Object References with getattr". First, it say in there that, "you can get a reference to a function without knowing its name until run−time, by using the getattr function". That is the first thing that is a little confusing as it doesn't really elaborate on that too much, that I can tell. What does that mean? I know that you can do:

Expand|Select|Wrap|Line Numbers
  1. getattr({}, "clear)
  2.  
and that will tell you that it is the clear function of a dictionary. But, what does that have to do with not knowing the function name till runtime?

Next, if you look at example 4.10, there is a line of code in there as follows:

Expand|Select|Wrap|Line Numbers
  1. getattr(li, "append")("Moe")
  2.  
and in the text below the examples section it says:

Expand|Select|Wrap|Line Numbers
  1. In case it hasn't sunk in just how incredibly useful this is, try this: the return value of getattr is the method, which you can then call just as if you had said li.append("Moe") directly. But you didn't call the function directly; you specified the function name as a string instead.
  2.  
To tell you the truth, it hasn't really sunk in, even yet. First, it says the return value is the method (append) and that you can then call it as if you had done an:

Expand|Select|Wrap|Line Numbers
  1. li.append("Moe")
  2.  
Why not just do the direct call? Why go through all the trouble of the getattr() function?

I guess what I am getting at is, what is the getattr() function really useful for, because right now, I just don't see it? Can someone please explain it in a way that will allow me to see its full usefulness?

Sorry for the ignorance if this tends to be a clouded view!

Regards and Happy New Year!

Jeff
The cases in the book use strings that were statically typed in. However, those strings could just as easily be taken from user input.
Dec 31 '07 #2
bvdet
2,851 Expert Mod 2GB
I have used getattr() a few times. It is useful when you need to get an attribute or method of an object with a string variable or argument. In this example, we pass an 'x', 'y' or 'z' to a function that sorts a list of point objects on the appropriate attribute.
Expand|Select|Wrap|Line Numbers
  1. def sortPts(ptlist, key='x', reverse=False):
  2.     '''
  3.     Sort a point list on the 'x', 'y' or 'z' attribute
  4.     '''
  5.     try:
  6.         def cmpItems(a,b):
  7.             if reverse:
  8.                 return -(cmp(getattr(a, key.lower()), getattr(b, key.lower())))
  9.             return cmp(getattr(a, key.lower()), getattr(b, key.lower()))
  10.         ptlist.sort(cmpItems)
  11.     except AttributeError, e:
  12.         raise AttributeError, 'Error in sortPoints() - %s' % (e)
Jan 1 '08 #3
numberwhun
3,503 Expert Mod 2GB
Ok, I somewhat see it, but the fog is still clouding my understanding. I am going to continue reading and hopefully it will lift with further examples. Thank you to both of you for your help!

Regards and Happy New Year!

Jeff
Jan 1 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Jules Dubois | last post: by
7 posts views Thread by Rene Pijlman | last post: by
8 posts views Thread by Arvid Andersson | last post: by
40 posts views Thread by Steve Juranich | last post: by
5 posts views Thread by Daishi Harada | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.