Ver Mensaje Individual
  #2 (permalink)  
Antiguo 20/02/2013, 18:31
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: python convertir string a callable o funcion o metodo

getattr

Código Python:
Ver original
  1. class A:
  2.     def __init__(self, a):
  3.         self.a = a
  4.  
  5.     def some_method(self):
  6.         print self.a
  7.  
  8. x = A('a')
  9. y = A('b')
  10.  
  11. s = getattr(x, 'some_method')
  12. s()
  13.  
  14.  
  15. s = getattr(y, 'some_method')
  16. s()
  17.  
  18. try:
  19.     s = getattr(y, 'no_method')
  20. except AttributeError:
  21.     print "no_method no es un atributo"