試試這個:
class image:
def __init__(self, image):
self.img = image
def __repr__(self):
return repr([self.img])
希望這有助於!
---按要求---
好吧,我會盡我所能來解釋這段代碼是如何工作的。如果你曾經印製的一類對象 - 那麼你可能會得到一個輸出,看起來像這樣
<__main__.ExampleClass object at 0x02965770>
The __init__ function is a constructor method. In python there are several constructor methods which all have a prefix of __ and a suffix of __. These tell the interpreter how to handle to the object. For example there is a constructor method: __del__ which tells python what to do when you call del() on the object. Like this __repr__ is an constructor method too. 'repr' is short for represent - what python should represent the object - it's default value. Normally you would return a value without the repr() function. Repr() is a magic method (like del()) and what it does is it calls the __repr__ method of the object inside of the brackets. It must be known that each data type - variable, list, tuple, dictionary etc. Are actually instances of a class. Each data type has it's own __repr__ method - telling python how it should represent it, because remember on the computer everything is in binary. This means when you return the representation of the image, you don't return it as a string, but as an object.
我不是最好的是解釋,但希望這會清除一些東西了。如果任何人有更好的方式解釋這一點,請繼續。
你必須確保你的圖像類是[array_like](https://stackoverflow.com/questions/40378427/numpy-formal-definition-of-array-like-objects)。 – user8153
根據'image'類的複雜程度,你確定你甚至需要一個單獨的類嗎?如果只有一個屬性,只需要有數組和一些附加功能可能會更簡單。 –
我發現你的代碼令人困惑,因爲名稱不一致。我做了一些編輯,使名稱更加有意義的不同。請檢查我的編輯是否沒有意外引入錯誤。 – trentcl