What is getattr() exactly and how do I use it?

0 votes

I was reading about the getattr() function. The problem is that I still can't grasp the idea of its usage. The only thing I understand about getattr() is that getattr(li, "pop") is the same as calling li.pop.

I didn't understand when the book mentioned how you use it to get a reference to a function without knowing its name until run-time. Maybe this is me being a noob in programming, in general. Could anyone shed some light to the subject? When and how do I use this exactly?

Sep 17, 2018 in Python by bug_seeker
• 15,350 points

1 answer to this question.

0 votes

You can view a full example here:

Introspection can be used for different purpose, the one presented in 'Dive Into Python' is simply a way to add functionality (plug-in) dynamically in your application.

By dynamically I mean without doing modification in the core application to add a new feature.

Taking the 'Dive Into Python' example - a simple app to extract attribute from file of different file - you can add the handling of a new file format without doing modification to the original application.

I recommend that you finish the book. As you read, everything will become increasingly clear.

answered Sep 17, 2018 by bug_seeker
• 15,350 points

Related Questions In Python

0 votes
3 answers
0 votes
1 answer
0 votes
0 answers

what is python reduce? how do you use it?

can you give an example? READ MORE

May 9 in Python by Waseem
• 4,470 points
0 votes
1 answer

How do I use urllib to see if a website is 404 or 200 in Python?

For Python 3, try doing this: import urllib.request, ...READ MORE

answered Nov 29, 2018 in Python by Nymeria
• 3,520 points

edited Dec 11, 2018 by Nymeria 1,056 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 329 views
+2 votes
2 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,350 points
0 votes
1 answer

When to use %r instead of %s in Python? [duplicate]

The %s specifier converts the object using ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,350 points