Unbound methods

Unbound methods - Python - Programmation

Marsh Posté le 08-09-2004 à 13:08:57    

imaginons

Code :
  1. class foo:
  2.     def bar(self):
  3.          blabla
  4. [...]


 
faire un fb = foo() me permet d'appeler fb.bar()
 
Mais est-il possible d'appeler cette fonction par foo.bar() (donc sans l'avoir rattachée à une instance de foo)?
J'ai tenté sans paramètre self, sans succès, j'ai tenté en lui fournissant une instance de foo en paramètre self, mais elle l'utilise pour se binder...


---------------
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 by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
Reply

Marsh Posté le 08-09-2004 à 13:08:57   

Reply

Marsh Posté le 08-09-2004 à 13:25:33    

ben si tu mets self, c'est une méthode d'instance
 
sinon voir classmethod/staticmethod

Reply

Marsh Posté le 08-09-2004 à 13:28:12    

Taz a écrit :

ben si tu mets self, c'est une méthode d'instance


Je sais, mais sans le self il me balance

Code :
  1. >>> class foo:
  2. ...     def bar():
  3. ...             print 1
  4. ...
  5. >>> foo.bar()
  6. Traceback (most recent call last):
  7.   File "<stdin>", line 1, in ?
  8. TypeError: unbound method bar() must be called with foo instance as first argument (got nothing instead)


Code :
  1. sinon voir classmethod/staticmethod


Ok, j'essaierais de voir ce truc


---------------
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 by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
Reply

Marsh Posté le 08-09-2004 à 13:31:48    

ou alors les décorator si tu bosses avec python2.4

Reply

Marsh Posté le 08-09-2004 à 13:36:50    

2.3.4 pour le moment
 
tu pourrais m'en dire plus sur les décorators ou tu m'envoies dans les docs?


---------------
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 by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
Reply

Marsh Posté le 08-09-2004 à 13:38:51    

ben tu vas sur python.org

Reply

Sujets relatifs:

Leave a Replay

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