adding all files done so far
This commit is contained in:
37
learning/descriptors/descriptors2.py
Normal file
37
learning/descriptors/descriptors2.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from weakref import WeakKeyDictionary
|
||||
|
||||
|
||||
class Drinker:
|
||||
def __init__(self):
|
||||
self.req_age = 21
|
||||
self.age = dict()
|
||||
print('init')
|
||||
|
||||
def __get__(self, instance_obj, objtype):
|
||||
print(f'{instance_obj}, {objtype}, {self.age}')
|
||||
return self.age.get(instance_obj, self.req_age)
|
||||
|
||||
def __set__(self, instance, new_age):
|
||||
if new_age < 21:
|
||||
msg = '{name} is too young to legally imbibe'
|
||||
raise Exception(msg.format(name=instance.name))
|
||||
self.age[instance] = new_age
|
||||
print('{name} can legally drink in the USA'.format(
|
||||
name=instance.name))
|
||||
|
||||
def __delete__(self, instance):
|
||||
del self.age[instance]
|
||||
|
||||
|
||||
class Person:
|
||||
drinker_age = Drinker()
|
||||
|
||||
def __init__(self, name, age):
|
||||
self.name = name
|
||||
self.drinker_age = age
|
||||
|
||||
|
||||
p = Person('Miguel', 30)
|
||||
p1 = Person('Bob', 50)
|
||||
|
||||
p = Person('Niki', 13)
|
||||
Reference in New Issue
Block a user