[英]Properties in IronPython: eternal loop
好吧,我對python非常了解,現在我嘗試將c#代碼轉換為IronPython。 有屬性問題:
這是我的課程,例如(從http://www.programiz.com/python-programming/property上獲得它):
class SomeClass(object):
def __init__(self, temperature = 0):
self._temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
@property
def temperature(self):
print("Getting value")
return self.temperature
@temperature.setter
def temperature(self, value):
print("Setting value")
self.temperature = value
當我嘗試從另一個這樣的類獲取此屬性時
cb = SomeClass()
temp = cb.to_fahrenheit()
我通過打印“獲取值”和最后的StackOverflowException進行了永恆的調用。 屬性有什么問題? 這是翻譯帶有ref參數的getter和setter的小任務,但是我無法繼續執行此錯誤。
你的getter和setter應該是返回/設置自我。 _溫度(背景字段),而不是self.temperature(屬性)-通過自我引用,您造成了無休止的循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.