Diskuze: Python 3.8 deskriptor
Zobrazeno 3 zpráv z 3.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
Diky za postreh, je to tak.
Tohle uz funguje:
class Typed:
def __init__(self, ty=int):
self.ty = ty
self.dictionary = WeakKeyDictionary()
def __get__(self, instance, owner=None):
print("Getting instance")
return self.dictionary[instance]
def __set__(self, instance, value):
print("Setting instance", instance, "to", value)
if not isinstance(value, self.ty):
raise TypeError("Value must be type {}".format(self.ty))
self.dictionary[instance] = value
Zobrazeno 3 zpráv z 3.