Python动态实例化类

  反射机制的工厂方法?反射机制的策略模式?

import sys

def func1():
	print('func1')

class TestClass():
	def p(self):
		print('you got me!')

def Main():
	className = 'TestClass'
	funcName = 'func1'

	testClass = globals()[className]()
	testClass.p()
	
	testFunc = globals()[funcName]
	testFunc()

Main()
发布了31 篇原创文章 · 获赞 11 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/MrRight17/article/details/82051369