1 class Person(): 2 def __init__(self, name): 3 self.name = name 4 5 6 def print_name(self): 7 print(self.name) 8 9 p = Person(‘Li‘) 10 import types 11 p.print_name = types.MethodType(print_name, p) # 绑定函数到对象 12 p.print_name() 13 14 15 @staticmethod 16 def print_abc(): 17 print(‘abc‘) 18 19 Person.print_abc = print_abc 20 Person.print_abc() 21 22 23 @classmethod 24 def print_123(cls): 25 print(‘123‘) 26 27 Person.print_123 = print_123 28 Person.print_123()
原文地址:https://www.cnblogs.com/gundan/p/8151177.html
时间: 2024-11-08 17:23:17