forked from CodeMouse92/DeadSimplePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllama.py
More file actions
90 lines (68 loc) · 2.18 KB
/
llama.py
File metadata and controls
90 lines (68 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Quadraped:
leg_count = 4
def __init__(self, species):
self.species = species
class Llama(Quadraped):
"""A quadraped that lives in large rivers."""
dangerous = True
def __init__(self):
self.swimming = False
super().__init__("llama")
def warn(self):
if self.swimming:
print("Cuidado, llamas!")
@classmethod
def feed(cls):
print("Eats honey with beak.")
llama = Llama()
from pprint import pprint
from xml.dom.minidom import Attr
print("Instance.__dict__:")
pprint(vars(llama))
print("\nLlama class __dict__:")
pprint(vars(Llama))
print("\nQuadruped class __dict__")
pprint(vars(Quadraped))
llama = Llama()
try:
print(object.__getattribute__(llama, 'swimming'))
except AttributeError as e:
try:
__getattr__ = object.__getattribute__(llama, '__getattr__')
except AttributeError:
raise e
else:
print(__getattr__(llama, 'swimming'))
try:
print(type.__getattribute__(Llama, 'leg_count'))
except AttributeError as e:
try:
__getattr__ = type.__getattribute__(Llama, '__getattr__')
except AttributeError as e:
try:
__getattr__ = type.__getattribute__(Llama, '__getattr__')
except AttributeError:
raise e
print(__getattr__(Llama, 'leg_count'))
# Either of these works!
print(llama.swimming) # prints 'False'
print(getattr(Llama, 'leg_count')) # prints '4'
if hasattr(llama, 'larger_than_frogs'):
print("¡Las llamas son más grandes que las ranas!")
try:
getattr(llama, 'larger_than_frogs')
except AttributeError:
pass
else:
print("¡Las llamas son más grandes que las ranas!")
setattr(llama, 'larger_than_frogs', True)
print(llama.larger_than_frogs) # prints 'True'
setattr(Llama, 'leg_count', 3)
print(Llama.leg_count) # prints '3'
Llama.dangerous = False # this is better
print(llama.dangerous) # prints 'False', looks OK?
print(Llama.dangerous) # prints 'False', we are safe now
print(llama.larger_than_frogs) # prints 'True'
# del llama.larger_than_frogs
delattr(llama, 'larger_than_frogs')
print(llama.larger_than_frogs) # raises AttributeError