Well, code speaks more (I have hard-coded some things, to isolate the problem and make the question shorter):
class wrapper:
def __init__( self, func ):
self.func = func
def __call__( self, *args ):
print( "okay, arg = ", args[0] )
self.func( self, args )
class M( type ):
def __new__( klass, name, bases, _dict ):
_dict[ "f" ] = wrapper( _dict[ "f" ] )
return type.__new__( klass, name, bases, _dict )
class AM( metaclass = M ):
def __init__( self ):
self.a = 0
def f( self, a ):
self.a = a
am = AM()
print( am.a ) # prints 0, expected
am.f( 1 ) # prints: "okay, arg = 1"
print( am.a ) # prints 0 again, also expected
I want the second print to show 1, instead of 0. In other words, is it possible, and if so – how, to pass the “real self” to my wrapper?
Note: I know why this prints 0 and I know what is the problem here ( wrapper‘s self is passed, instead of the object, that calls f), but I don’t know how to solve it.
Any ideas?
EDIT – thanks all for the answers, +1 from me. But I think I need to do this with class, as I need to store some additional info (like metadata) (this is simplified version of my real problem). Is it possible and how, if so? Sorry for not specifying this at the very beginning.
Make
wrappera descriptor so that you know the specific instance being poked.