Voici un exemple assez simple d'un décorateur qui pourrait permettre d'appeler une méthode c++ au lieu d'une méthode python :
def cpp(function):
def my_method(instance, *args, **kwargs):
print "Calling cpp version of method %s" % my_method.__name__
print "On instance", instance
print "With args", args
print kwargs
my_method.__name__ = function.__name__
my_method.__doc__ = "C++ version of " + function.__name__
my_method.__doc__ += "\n" + function.__doc__
return my_method
class A:
def __init__(self, value):
self.value = value
@cpp
def show(self):
""" Affiche la valeur """
pass
a = A(50)
a.show(10, 12, value="")
0 commentaires:
Enregistrer un commentaire