2013-07-17 91 views
0

這應該很簡單,但我無法找到正確的Google短語來找到它。只需要在def loadClass()中查找所需的代碼,不知道它會是什麼。提前致謝!以用戶輸入來加載課程

class ClassA(): 
    def __init__(self,date): 
     pass 
class ClassB(): 
    def __init__(self,date): 
     pass 
def loadClass(className,date): 
    loadedClass = className(date) 

loadClass(ClassA,'1/1/2013') 
loadClass(ClassB,'1/2/2013' 
+1

嗯'返回className(日期)'? –

回答

2

如果你硬是以一個字符串作爲類名,然後你可以生成「類名」爲「類對象」映射的查找和使用:

allowed_classes = { 
    'A': ClassA, 
    'B': ClassB 
} 

def load_class(class_name, *args, **kwdargs): 
    return allowed_classes[class_name](*args, **kwdargs) 

new_class_obj = load_class('B', 'Jan 1st')