Need help to understand a getattr syntax.

Diez B. Roggisch deets at nospam.web.de
Tue Nov 24 10:51:55 EST 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)

Diez



More information about the Python-list mailing list