On Jul 5, 1:52 am, Alex Popescu <[EMAIL PROTECTED]> wrote: > Hi all! > > I am pretty new to Python, so please excuse me if I am missing > something. Lately, I've been playing with decorators and I am a bit > confused about some behavior. Here is the code that puzzles me: > > in python shell: > > def function(): > pass > > class A(object): > def method(self): > pass > > from types import MethodType > from types import FunctionType > > if type(function) is FunctionType: > print "this is what I expect" > > if type(A.method) is MethodType: > print "this is what I expect" > > so far so good... everything seems logical. > > But if a decorator is declared things are becoming different: > > def deco(function): > if type(function) is MethodType: > print "MethodType" > elif type(function) is FunctionType: > print "FunctionType" > > @deco > def function2(): > pass > > # ==> this prints out FunctionType (oke) > > class A(object): > @deco > def method(self): > pass > > # ==> this prints out FunctionType (???) > > Can somebody shed some light on why I am seeing this? > > (Using Python 2.5.1 on Win XP). > > TIA, > > ./alex > -- > .w( the_mindstorm )p.
First of all I should correct my example to use isinstance(function_or_method, builtin_factory_function). Secondly, I guess what is happening is the following: - when testing from outside the class definition, the function is already attached to the class instance and this is the reason why its type is instancemethod - for decorators, my test is executed before the class is defined and so at that moment the function is still a function; it will become a methodinstance only after the class definition is closed Is this correct or am I just fabulating here? TIA, ./alex -- .w( the_mindstorm )p. -- http://mail.python.org/mailman/listinfo/python-list