Need help to understand a getattr syntax.

N

NMarcu

Hello all,
I need some help to understand a getattr syntax. The syntax is:

try:
getattr(self, command)(cursor, row)
except Exception, e:
print "Action failed : '%s'" % command
raise e

I don't understand why is not like this: a = getattr(), and what are
the scope of the second (): (cursor, row)

Thanks.
 
D

Diez B. Roggisch

NMarcu said:
Hello all,
I need some help to understand a getattr syntax. The syntax is:

try:
getattr(self, command)(cursor, row)
except Exception, e:
print "Action failed : '%s'" % command
raise e

I don't understand why is not like this: a = getattr(), and what are
the scope of the second (): (cursor, row)

The attribute in question is a callable, a method most probably. So

getattr(self, command)

returns a reference to the method, and the following

(cursor, row)

calls it with cursor and row as parameters.

You could rewrite it as

method = getattr(self, command)
print method
method(cursor, row)

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top