getattr problem

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Dec 1 14:17:39 EST 2009


Victor Subervi wrote:
> Hi;
> I have the following code that execute without a problem:
>
> import sys,os
> sys.path.append(os.getcwd())
> import options
> storesTables = []
> junkStores = string.join(addStore(), ', ')
> for table in optionsTables():
>   if table not in ('particulars', junkStores):
>     storesTables.append(table)
> for table in storesTables:
>   try:
>     fn = getattr(options, table)
>     print fn()
>   except:
>     pass
>
> I need to change the obvious line to this or something similar (that 
> actually works):
>
>     fn = getattr(options, '%s("names")' % table)
>
> That is, I need to pass the variable "names" to each table as it is 
> called. How do I do this?
> TIA,
> Victor
I'm not sure I understood what your are trying to do.

  try:
    fn = getattr(options, table)
    fn(names)
  except:
    pass

Coud be the answer, providing that table is a callable object.

But I dont see any variable names defined in your code example, so I'm a 
little bit confused


JM



More information about the Python-list mailing list