Le 19/04/2020 à 17:06, ast a écrit :

I understood where the problem is.

Once __get__ is defined, it is used to read self._name in method
__set_name__ and it is not défined yet. That's why I have
an error "'NoneType' object is not callable"
Thats a nice bug.
I need to think for a workaround

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