descriptor.__get__(self, obj, objtype=None)
-> valuedescriptor.__set__(self, obj, value)
-> Nonedescriptor.__del__(self, obj)
-> None__get__
jest nazywany "non-data descriptor"
In [ ]:
class Punkt():
def __init__(self, x, y):
self._x = x
self._y = y
def getX(self):
return self._x
def getY(self):
return self._y
def setX(self, x):
self._x = x
def setY(self, y):
self._y = y
p = Punkt(1, 2)
print(p.getX())
p.setX(5)
print(p.getX())
In [ ]:
class Punkt():
def __init__(self, x, y):
self._x = x
self._y = y
def getX(self):return self._x
def setX(self, x):self._x = x
def getY(self):return self._y
def setY(self, y):self._y = y
x = property(fget=getX, fset=setX, fdel=None, doc=None)
y = property(getY, setY)
p = Punkt(1, 2)
print(p.x)
p.x = 5
print(p.x)
In [ ]:
class Punkt():
def __init__(self, x, y):
self._x = x
self._y = y
def getX(self):return self._x
def setX(self, x):self._x = x
def getY(self):return self._y
def setY(self, y):self._y = y
def dodaj(self):
return self._x + self._y
x = property(getX, setX)
y = property(getY, setY)
suma = property(dodaj)
p = Punkt(1, 2)
print(p.x)
p.x = 5
print(p.x)
print(p.suma)
In [ ]:
class ReadOnly():
def __init__(self, value):
self._value = value
def __get__(self, obj, objtype):
print(self, obj, objtype)
print(obj._y)
return self._value
def __set__(self, obj, val):
print(self, obj, val)
raise AttributeError
class Punkt():
x = ReadOnly(5)
def __init__(self, y):
self._y = y
p = Punkt(2)
print(p.x)
p.x = 10
In [ ]:
class ReadOnly():
def __init__(self, value):
self._value = value
def __get__(self, obj, objtype):
print("get: ", self, obj, objtype)
print(obj._y)
return self._value
def __set__(self, obj, val):
print(self, obj, val)
raise AttributeError
class Punkt():
def __init__(self, x, y):
self.x = ReadOnly(x)
self._y = y
p = Punkt(1, 2)
print(p.x)
p.__dict__
p.__dict__['x']
In [ ]:
class ReadOnlyXPlus5():
def __get__(self, obj, objtype):
print("get: ", self, obj, objtype)
return obj._x + 5
def __set__(self, obj, val):
print(self, obj, val)
raise AttributeError
class Punkt():
def __init__(self, x, y):
self._x = x
self._y = y
x = ReadOnlyXPlus5()
p = Punkt(1, 2)
print(p.x)
print(p.__dict__)
Punkt.x
In [ ]:
class NonDataDesc():
def __init__(self, x):
self.x = x
def __get__(self, obj, objtype):
print("NonDataGet")
return self.x
class DataDesc():
def __init__(self, x):
self.x = x
def __get__(self, obj, objtype):
print("DataDescGet")
return self.x
def __set__(self, obj, val):
self.x = val
class A():
#x = NonDataDesc(10)
x = DataDesc(-5)
a = A()
print(a.x)
a.x = 5
print(a.x)
__getattribute__
dla type i object__dict__
dict
obiektu__get__
-> wywołaj x.__get__(obj)
object.__getattribute__
b.x
-> type(b).__dict__['x'].__get__(b, type(b))
B.x
-> B.__dict__['x'].__get__(None, B)
In [ ]:
def foo():
pass
dir(foo)
In [ ]:
class A():
def __init__(self, x):
self.x = x
def f(*args):
print(args)
if len(args) > 0:
print(args[0].x)
print("foo")
A.f()
#A(5).f()