class A(object):
def __get__(self, instance, owner):#why i can't find argument 'key',where is 'key'
#print ower.instance
print instance,owner
def __set__(self,instance,value):
instance=value
class X(object):
a = A()
xx=X()
xx.a='aaa'
print xx.a#None
class A(object): def __get__(self, instance, owner):#why i can’t find argument ‘key’,where is ‘key’ #print
Share
Hettinger’s HowTo Guide for Descriptors covers this well. Quoting from it:
So, you can name the arguments however you wish, but there’s typically no argument named
keyto__get__(no idea why you’re trying to find it).Again an example from that URL:
class RevealAccess(object):
“””A data descriptor that sets and returns values
normally and prints a message logging their access.
“””
So normally you set
self.somethingin__init__(and/or__set__if you define it), and return something based onself.somethingin__get__. Of course this example justprints the “something” on getting and setting, normally you’d do something more substantial;-).