1. 程式人生 > >Python動態例項化類

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()