[Tutor] getattr help

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] getattr help

Michael Lewis
What is the purpose of getattr? Why not just use help or am I completely misunderstanding this?

>From what I read, getattr allows you to get a reference to a function without knowing its name until runtime.

However, the example provided is:

li = ['larry', 'curly]
getattr(li, 'pop')

It seems to me that I need to know the name of the function to use getattr?

Please help.

Sent from my iPhone
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] getattr help

Peter Otten
[hidden email] wrote:

> What is the purpose of getattr? Why not just use help or am I completely
> misunderstanding this?
>
>>From what I read, getattr allows you to get a reference to a function
>>without knowing its name until runtime.
>
> However, the example provided is:
>
> li = ['larry', 'curly]
> getattr(li, 'pop')
>
> It seems to me that I need to know the name of the function to use
> getattr?

The name need not be known *until* *runtime*. You can pass it as a variable
which makes the program more flexible. Here is an example:

$ cat getattrdemo.py
class Joe:
    def run(self):
        print("running")
    def jump(self):
        print("jumping")
    def say_hello(self):
        print("Hello, I am Joe")

class Sue:
    def say_hello(elf):
        print("Hello, I am Sue")
    def swim(self):
        print("swimming")

for person in [Joe(), Sue()]:
    person.say_hello()
    actions = [action for action in dir(person) if not
action.startswith("_")]
    print("I can", ", ".join(actions))
    while True:
        action = input("What do you want me to do? ")
        if action == "":
            print("bye")
            break
        if action in actions:
            getattr(person, action)()
        else:
            print("I'm afraid I can't", action)

The code in the for loop does not "know" what joe or sue can do, it detects
it at runtime. Therefore you don't have to change it when you add another
person (as long as it can .say_hello()).

And here's what you may see when you run the script:

$ python3 getattrdemo.py
Hello, I am Joe
I can jump, run, say_hello
What do you want me to do? jump
jumping
What do you want me to do? run
running
What do you want me to do? swim
I'm afraid I can't swim
What do you want me to do?
bye
Hello, I am Sue
I can say_hello, swim
What do you want me to do? say_hello
Hello, I am Sue
What do you want me to do? talk
I'm afraid I can't talk
What do you want me to do? swim
swimming
What do you want me to do?
bye
$

If you want to see a more advanced example of the technique, have a look at
cmd.py in Python's standard library.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor