Need help to understand a getattr syntax.

Diez B. Roggisch deets at
Tue Nov 24 16:51:55 CET 2009

NMarcu wrote:

> 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)


More information about the Python-list mailing list