Hello

This code is working well: (I am using python 3.6.3)

--------------------------------------------------------------------
class my_property:
    def __init__(self, fget=None, fset=None, fdel=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
    def __set_name__(self, owner, name):
        self._name = name
        print("Inside __set_name__ method:  ", self._name)

##    def __get__(self, instance, owner):
##        return self.fget(instance)


class Test:
    celsius = my_property()

print("Outside __set_name__ method: ", Test.celsius._name)


Here is the output:

========= RESTART: C:/Users/jm/Desktop/python/my_property.py =========
Inside __set_name__ method:   celsius
Outside __set_name__ method:  celsius
--------------------------------------------------------------------

And this one doesn't. (I just removed ## to uncomment the 2 lines)
Do you understand why ?

class my_property:
    def __init__(self, fget=None, fset=None, fdel=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
    def __set_name__(self, owner, name):
        self._name = name
        print("Inside __set_name__ method:  ", self._name)

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


class Test:
    celsius = my_property()

print("Outside __set_name__ method: ", Test.celsius._name)

Here is the output:

========= RESTART: C:/Users/jm/Desktop/python/my_property.py =========
Inside __set_name__ method:   celsius
Traceback (most recent call last):
  File "C:/Users/jm/Desktop/python/my_property.py", line 17, in <module>
    print("Outside __set_name__ method: ", Test.celsius._name)
  File "C:/Users/jm/Desktop/python/my_property.py", line 11, in __get__
    return self.fget(instance)
TypeError: 'NoneType' object is not callable
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to