![](/img/trans.png)
[英]how to call the Apache beam DoFn Class from another general class or vice versa?
[英]How to call classmethod of base class from derived class when base class itself is inheriting from another library class (ndb.Model)
我知道 python 中與類方法相關的問題已得到多次解答,但即使在實施這些解決方案后,我面臨的問題仍然存在。
我遇到了許多真正幫助我理解基本邏輯的答案,但由於這里的ndb.Model
實現,我發現很難查明問題所在。
我的代碼結構,
class A(ndb.Model):
@classmethod
def fun(cls, input):
db_obj = cls.get_by_id(input) # get_by_id is ndb.Model function that brings record from db
return db_obj.required_data
class B(A):
@classmethod
def fun(cls, input):
required_data = super(B, cls).fun(input)
當我們到達cls.get_by_id(input)
時,即使存在所需的記錄,也不會從 DB 返回任何內容。 當我不將派生的 class fun()
作為類方法並像A.fun()
那樣直接調用它時,它就可以工作了。 但是,我認為這不是正確的實施方式。
以上任何幫助都會很棒。 謝謝!
您嘗試檢索的數據存儲實體是 A 實體還是 B 實體?
我懷疑它是一個 B 實體,因為您是用B.fun()
開始這個過程的。
如果是這種情況,那么get_by_id
A.fun()
中的 get_by_id 將不起作用,因為它正在嘗試檢索 A 實體。 A.fun()
中的cls
是 A 而不是 B,因此它試圖獲取一個不存在的 A 實體。
一種解決方案是修改 A.fun 以同時采用指示要檢索的實體類型的參數。 這可能是 class 或只是一個字符串。 是這樣的:
class A(ndb.Model):
@classmethod
def fun(cls, input, cls2):
db_obj = cls2.get_by_id(input) # get_by_id is ndb.Model function that brings record from db
return db_obj.required_data
class B(A):
@classmethod
def fun(cls, input):
required_data = super(B, cls).fun(input, cls)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.