定義類,AttributeError: ‘tuple‘ object has no attribute ‘extra_fb1‘ 報錯及解決方案
阿新 • • 發佈:2021-02-15
技術標籤:筆記
最近在寫OpenMV程式碼,自己定義類的時候出現了AttributeError: ‘tuple’ object has no attribute ‘extra_fb1’ 的錯誤
錯誤程式碼:
class template: def __init__(self): self.extra_fb1 = sensor.alloc_extra_fb(sensor.width(),sensor.height(),sensor.GRAYSCALE) def up_date(img,roi,self): sensor.dealloc_extra_fb() self.extra_fb1 = sensor.alloc_extra_fb(roi[2],roi[3],sensor.GRAYSCALE) self.extra_fb1.replace(img.copy(roi=roi)) return self.extra_fb1
錯誤內容是說 tuple 類別沒有 extra_fb1方法
what???
我程式裡面extra_fb1是一個變數才對,怎麼會是一個方法呢???
系統怎麼會將self裡面的例項屬性理解成方法呢???
經過一番嘗試後發現,在類方法中,self是預設傳入的,並且是在“第一個位置”,也就是說如果我把self寫在了上面程式段中的roi引數後面,會把第一個傳進來的self覆蓋掉,導致原始的self消失。
解決方案:
把上面定義類方法的傳參形式改成下面的就好了
def up_date(img,roi,self):