在我的(第一個!)Python程序中有兩個函數,它們只因需要實例化的類而有所不同。我該如何給一個類作爲Python函數的參數
def f(id):
c = ClassA(id)
...
return ...
def g(id):
c = ClassB(id)
...
return ...
爲了避免重複的代碼,我希望能夠寫一個函數,它會以某種方式接受類實例化作爲參數。
def f(id):
return f_helper(id, ... ClassA ...)
def g(id):
return f_helper(id, ... ClassB ...)
def f_helper(id, the_class):
c = ... the_class ... (id)
...
return ...
我敢肯定,這是可能的,但怎麼沒發現?