Hello,

I am wondering why this code is OK:

class Temperature:
    def __init__(self):
        self.celsius = 0

    fahrenheit = property()
        
    @fahrenheit.getter
    def fahrenheit(self):
        return 9/5*self.celsius +32
        
    @fahrenheit.setter
    def fahrenheit(self, value):
        self.celsius = (value-32)*5/9


and this one is not:


class Temperature:
    def __init__(self):
        self.celsius = 0

    fahrenheit = property()
        
    @fahrenheit.getter
    def f(self):
        return 9/5*self.celsius +32
        
    @fahrenheit.setter
    def f(self, value):
        self.celsius = (value-32)*5/9

In the second code, I just changed the names of the
decorated functions

Unforunately I was not able to find "property" source
code to understand how it works exactly

I wrote a my_property which makes the two previous codes
to work fine. This is why I don't understand why the
genuine property does't work when decorated functions
are named f rather than fahrenheit

Here it is:

class my_property:

    def __init__(self, fget=None, fset=None, fdel=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel

    def __get__(self, instance, owner):
        if instance is None:
            return self
        return self.fget(instance)

    def __set__(self, instance, value):
        self.fset(instance, value)

    def __delete__(self, instance):
        self.fdel(instance)

    def getter(self, fget):
        self.fget = fget
        return self

    def setter(self, fset):
        self.fset = fset
        return self

    def deleter(self, fdel):
        self.fdel = fdel
        return self
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to