Not understood error with __set_name__ in a descriptor class
ast
ast at invalid
Sun Apr 19 11:06:41 EDT 2020
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
More information about the Python-list
mailing list