0
我正在尋找關於在Python對象中自動驗證實例變量的建議。實例化或設置時應立即進行驗證。驗證Python類數據的建議?
例如,想像這樣一類:
import recordtype
class TemperatureReading(recordtype.recordtype('TemperatureReading',
['lat','long','alt','temp'])):
pass
我想能夠約束LAT至+/- 90,長至+/- 180,ALT> = 0,和臨時> = 0。
寫入getter和setter似乎太不pythonic和繁瑣。有沒有更好的辦法?
不知道我明白。你不能在'__init__'中進行驗證,或者你想要驗證在用戶嘗試執行像'reading.lat = 120'這樣的事情時發生嗎?爲此你可以使用'property'。 – mgilson