在python中类可以利用from types import MethodType语句调用类外函数
例:
class Cat(object):
def __init__(self):
super().__init__()
def run(self):
print("猫在奔跑")
main.py:
from demo01 import *
from types import MethodType
def main():
cat01=Cat()
cat01.run=MethodType(run,cat01)
cat01.run()
if __name__=="__main__":
main()
示例中我创建了一个Cat类,然后在Cat类外写了一个run函数,然后再主函数中我调用MethodType函数使得Cat类可以使用类外函数打印输出“猫在奔跑”