about the function in class

xuanwu348 xuanwu348 at 163.com
Fri Jul 17 14:05:24 EDT 2020


Hi, all


There are some methods to define functions in a class, include member functions, static method, class method. 


My question is that which type of function for  "decorate(f),diff_con(f)" 


I think these functions all were  belong to member function, (f <-->self) f can be replace by self.


but how to explain "decorate(f)", "@decorate(f)"
if f == self; then in wrapper function: res = self(*args, **kwargs)
and self means Diff_methods("somgthing") <==> Diff_methods("somgthing")(*args, **kwargs)
But it's not correct. 


thanks
lass Diff_methods:

def __init__(self, something):
self.something = something

def decorate(f):
@wraps(f)
def wrapper(*args, **kwargs):
            start_time = time.time()
            res = f(*args, **kwargs)
            elapse = time.time() - start_time
return res, elapse
return wrapper

def diff_con(f):
print(f.something)
@decorate
def member_function(self, n):
return 2 << n

@staticmethod
def static_function():
pass

@classmethod
def class_method(cls):
pass

if __name__ == "__main__":

sssss = Diff_methods("something")
print(sssss.member_function(10))
sssss.diff_con()



More information about the Python-list mailing list