[python] liste d'arguments de fonction ?

liste d'arguments de fonction ? [python] - Python - Programmation

Marsh Posté le 26-06-2007 à 16:47:25    

Hello,
je voudrais savoir comment lister les arguments definis pour une fonction ou objet sans trop toucher a mon code. je m'explique:
 

Code :
  1. def maFonction(bla, ble, bli):
  2.     for arg in args:
  3.         print arg


voila je voudrais avoir une liste ou quoi que ce soit des arguments de la fonction histoire de pouvoir avoir un resultat genre:
 
bla = telle_valeur
ble = telle_autre
bli = pouetpouet
 
en fait, je cherche a faire une objet de debug simple, a mettre dans toutes les fonctions de mon code.
cet objet loguerait le nom de la fonction et tous les arguments passes.
ce qui donnerait au final:
 

Code :
  1. debugFunction(debug_type, debug_inf):
  2.     if debug_type == "functioncall":
  3.         print "called "+debug_inf["function_name"]+"("+str(debug_inf["arguments"])+" )"
  4. def maFonction(arg1, arg2):
  5.     debugFunction("functioncall",["function_name":"maFonction","arguments":args])
  6.     # corps de la fonction
  7.     pass


 
Help, comment faire pour ne pas devoir entrer a la mano la liste des arguments definis pour la fonction ?

Reply

Marsh Posté le 26-06-2007 à 16:47:25   

Reply

Marsh Posté le 26-06-2007 à 21:56:23    

locals()

 

ou, encore mieux, un décorateur si tu es en Python 2.4 ou 2.5


Message édité par masklinn le 26-06-2007 à 21:58:03

---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 27-06-2007 à 15:53:02    

c koi un decorateur ?????

Reply

Marsh Posté le 27-06-2007 à 16:44:33    

RTFM


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 27-06-2007 à 21:05:16    

ok j'ai vu que ca a l'air pas mal,
l'exemple en particulier qui m'interesse est celui la:

Code :
  1. import sys
  2. WHAT_TO_DEBUG = set(['io', 'core'])  # change to what you need
  3. class debug:
  4.     """ Decorator which helps to control what aspects of a program to debug
  5.     on per-function basis. Aspects are provided as list of arguments.
  6.     It DOESN'T slowdown functions which aren't supposed to be debugged.
  7.     """
  8.     def __init__(self, aspects=None):
  9.         self.aspects = set(aspects)
  10.     def __call__(self, f):
  11.         if self.aspects & WHAT_TO_DEBUG:
  12.             def newf(*args, **kwds):
  13.                 print >> sys.stderr, f.func_name, args, kwds
  14.                 f_result = f(*args, **kwds)
  15.                 print >> sys.stderr, f.func_name, "returned", f_result
  16.                 return f_result
  17.             newf.__doc__ = f.__doc__
  18.             return newf
  19.         else:
  20.             return f
  21. @debug(['io'])
  22. def prn(x):
  23.     print x
  24. @debug(['core'])
  25. def mult(x, y):
  26.     return x * y
  27. prn(mult(2,2))


par contre je suis entraind e faire une appli multithreadee donc je ne sais pas comment je peux gerer ca e l'indentation d'appels recursifs de fonctions.
???

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed