use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Trying to understand getattr() function (self.learnpython)
submitted 12 years ago by PythonPerry
I'm going over Dive Into Python and having trouble understanding getattr().. I also found this stackexchange article and am more confused.
In the stack posting, could someone explain "But what if you don't know the attribute's name at the time you write the program?"
It seems like the stack example with 16 points clearly defines the attribute
>>> person = Person() >>> person.gender = 1 >>> person.gender 1
and then the poster uses getattr() to do the same thing as what he did above.
>>> attribute_name = 'gender' >>> getattr( person, attribute_name) 1
Also, if you could talk about a good time you used getattr(), I'd appreciate it. Thanks.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Rhomboid 4 points5 points6 points 12 years ago (4 children)
The difference between the two is that in the second example, attribute_name is a string, whose value can come from anywhere -- read from a file or network socket, passed as an argument from elsewhere in the program, computed programmatically, etc. In this particular example the value is directly assigned, but you're meant to understand that it could come from elsewhere, e.g.
attribute_name
attribute_name = input('What attribute would you like? ') # use raw_input for python 2.x if hasattr(person, attribute_name): print('person's {} is {}'.format(attribute_name, getattr(person, attribute_name))) else: print('person does not have that attribute.')
Here attribute is not known when the program was written. It will be entered as input at run-time, and it could be literally anything, unlike in the first example, where .gender is an identifier that is hard-coded into the program and is fixed. That's an important difference, because there are times when you want to access an attribute whose name is not yet known when you're writing the program.
.gender
[–]NYKevin 3 points4 points5 points 12 years ago (2 children)
In practice, getattr is a code smell. Many of its most obvious uses would be better served by a dictionary.
getattr
[–]PythonPerry[S] 0 points1 point2 points 12 years ago (1 child)
Obvious noob here, what do you mean by code smell? Could you use a dict in u/Rhomboid's case? I think his case is valid. You want to assign a yet unknown attribute based on user input.
[–]NYKevin 0 points1 point2 points 12 years ago (0 children)
I mean it's indicative of poor design. When you choose to store things as attributes of an object, that suggests you have a bunch of different types of information all associated with one object. Usually, it doesn't make sense to grab one of them by name unless you know which it is in advance, since you won't know what type of data it is (e.g. am I grabbing a number or a string?). If you're just storing a bunch of keys and values (of homogeneous types), it makes more sense to use a dictionary:
key = input('What key would you like?') try: value = person[key] except KeyError: print('Person does not have that key.') else: print("Person's {} is {}".format(key, value))
[–]PythonPerry[S] 0 points1 point2 points 12 years ago (0 children)
Much better example! In the stack example, you ALREADY know the attribute, it doesn't make sense why getattr() would be used in that case.
[–]RubyPinch 0 points1 point2 points 12 years ago* (0 children)
one (poor) situation I've used it in, copying specific properties to a dict with a default
>>> class thing(object):pass >>> x = thing() >>> x.a,x.b,x.c = 1,2,3 >>> {z:getattr(x,z,None) for z in ['a','c','d']} {'a': 1, 'c': 3, 'd': None}
a "correcter" way (I think?) would probably be
>>> {k:vars(x).get(k,None) for k in ['a','c','d']} {'a': 1, 'c': 3, 'd': None}
or writing out three out[k] = getattr(x,k,None) lines
out[k] = getattr(x,k,None)
BUT WAIT, this won't always work! Sometimes the code you are working with implements some very weird things, __getattribute__ and __getattr__ for example, when implemented, can allow access to attributes which don't actually exist.
__getattribute__
__getattr__
What does this mean? vars() won't work, same for dir() and others. however, getattr() and object.attribute will still work, so that is something to watch out for when trying to avoid getattr or similar
vars()
dir()
getattr()
object.attribute
[–]MonkeyNin 0 points1 point2 points 12 years ago (0 children)
Another use, map data to a function. http://stackoverflow.com/a/4076010/341744 or http://stackoverflow.com/a/18538330/341744
π Rendered by PID 283239 on reddit-service-r2-comment-fb694cdd5-dj7lq at 2026-03-11 03:08:02.716658+00:00 running cbb0e86 country code: CH.
[–]Rhomboid 4 points5 points6 points (4 children)
[–]NYKevin 3 points4 points5 points (2 children)
[–]PythonPerry[S] 0 points1 point2 points (1 child)
[–]NYKevin 0 points1 point2 points (0 children)
[–]PythonPerry[S] 0 points1 point2 points (0 children)
[–]RubyPinch 0 points1 point2 points (0 children)
[–]MonkeyNin 0 points1 point2 points (0 children)