Update page 'Day 5 Homework - Karen'
@@ -0,0 +1,27 @@
|
|||||||
|
```
|
||||||
|
class Vault:
|
||||||
|
def __init__(self):
|
||||||
|
self._treasure = "gold"
|
||||||
|
self._attributes = dict(self.__dict__)
|
||||||
|
|
||||||
|
def __setattr__(self, name, value):
|
||||||
|
if name != "_attributes" and name not in self._attributes:
|
||||||
|
print("attribute deleted!")
|
||||||
|
self._attributes[name] = value
|
||||||
|
super().__setattr__("_attributes", dict(self.__dict__))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def treasure(self):
|
||||||
|
if self.__dict__ != self._attributes:
|
||||||
|
print("attribute deleted!")
|
||||||
|
return self._treasure
|
||||||
|
```
|
||||||
|
To assess other's Vault (although it may not be ethical), we can use "__getattribute__" method like:
|
||||||
|
|
||||||
|
```
|
||||||
|
class EvilVault(Vault):
|
||||||
|
def __getattribute__(self, name):
|
||||||
|
if name == "treasure":
|
||||||
|
return "gold"
|
||||||
|
return super().__getattribute__(name)
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user