Why do managed attributes just work for class attributes and not for instance attributes in python?

后端 未结 3 1272
野的像风
野的像风 2021-01-15 00:00

To illustrate the question check the following code:

class MyDescriptor(object):
  def __get__(self, obj, type=None):
    print \"get\", self, obj, type
             


        
3条回答
  •  时光取名叫无心
    2021-01-15 00:36

    You should read this and this.

    It overwrites the function because you didn't overload the __set__ and __get__ functions of SomeClass but of MyDescriptor class. Maybe you wanted for SomeClass to inherit MyDescriptor? SomeClass1 prints the "get" and "set" output because it's a static method AFAIK. For details read the upper links.

提交回复
热议问题