ArgorAvest
ArgorAvest

Reputation: 151

Properties in IronPython: eternal loop

Well, I am very noob in python and now I try to translate c# code into IronPython. Have problem with properties:

Here is my class for example (got it from 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

When I try to get this property from another class like this

cb = SomeClass()
temp = cb.to_fahrenheit()

I got an eternal invocation with printing "Getting value" and finally StackOverflowException. What's wrong with properties? This is a little piece of task for translating getters and setters with ref parameters, but I cannot go ahead with this error. enter image description here

Upvotes: 2

Views: 344

Answers (1)

Misza
Misza

Reputation: 665

Your getter and setter should be returning/setting self._temperature (the backing field), not self.temperature (the property) - by self-referencing, you caused an endless loop.

Upvotes: 4

Related Questions