-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreflection.py
233 lines (166 loc) · 6.84 KB
/
reflection.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'''Reflection & Introspection'''
# A Python script can find out about the type, class, attributes and methods
# of an object. This is referred to as reflection or introspection. Functions
# include type(), isinstance(), callable(), dir() and getattr() and more.
# type()
# -----------------------------------------------------------------------------
import datetime
now = datetime.datetime.utcnow()
type(now) # <class 'datetime.datetime'>
type(234) # <class 'int'>
type('hello') # <class 'str'>
# bool()
# -----------------------------------------------------------------------------
# Will tell you whether something is True or False:
x = []
y = ['one']
z = None
bool(x) # False
bool(y) # True
bool(z) # False
# isinstance()
# -----------------------------------------------------------------------------
# because everything in Python is an object, isinstance works everywhere:
isinstance(now, datetime.datetime) # True
isinstance('hello', str) # True
isinstance(234, int) # True
isinstance(234, float) # False
isinstance(y, list) # True
# NOTE: As of Python 3.10, you can use a union operator '|' in isinstance():
# isinstance(1, int | str)
# True
# issubclass()
# -----------------------------------------------------------------------------
# The issubclass() function checks if the object (first argument) is a subclass
# of a class (second argument). You can also provide a tuple as the second
# argument. issubclass() will return True if at least one of the items in the
# tuple is True:
class Polygon():
def __init__(self, polygon_type):
print('This polygon is a', polygon_type)
class Triangle(Polygon):
def __init__(self):
super().__init__('triangle')
t = Triangle() # This polygon is a triangle
print(issubclass(Triangle, Polygon)) # True
print(issubclass(Triangle, list)) # False
print(issubclass(Triangle, (list, Polygon))) # True
# NOTE: As of Python 3.10, you can use a union operator '|' in issubclass():
# issubclass(Triangle, list | Polygon)
# True
# repr()
# -----------------------------------------------------------------------------
# returns a printable representation of the given object:
print(now) # 2017-11-17 18:44:35.002758
print(repr(now)) # datetime.datetime(2017, 9, 5, 18, 23, 30, 607281)
# As a side note, you can customize what repr() outputs in a class by using
# its magic method. See magic_methods.py
# callable()
# -----------------------------------------------------------------------------
# returns True if the object passed appears callable:
def test():
pass
callable(test) # True
# dir()
# -----------------------------------------------------------------------------
# tries to return a list of valid attributes of the object.
# show the names in the local module namespace:
print(dir())
# ['Polygon', 'Triangle', '__annotations__', '__builtins__', '__cached__',
# '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
# 'datetime', 'now', 't', 'test', 'x', 'y', 'z']
# show all the Built-in things:
for m in dir(__builtins__):
print(m, end=', ')
# show the names in the datetime module:
print(dir(datetime))
# ['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__',
# '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime',
# 'datetime_CAPI', 'time', 'timedelta', 'timezone', 'tzinfo']
# Print just the useful ones:
for m in dir(datetime.datetime):
if m[0] != '_':
print(m, end=', ')
# astimezone, combine, ctime, date, day, dst, fold, fromordinal, fromtimestamp,
# hour, isocalendar, isoformat, isoweekday, max, microsecond, min, minute,
# month, now, replace, resolution, second, strftime, strptime, time, timestamp,
# timetuple, timetz, today, toordinal, tzinfo, tzname, utcfromtimestamp,
# utcnow, utcoffset, utctimetuple, weekday, year
# getattr()
# -----------------------------------------------------------------------------
# returns the value of an attribute of an object, given the attribute name,
# but also lets you provide a default value to avoid raising an exception:
class Person:
age = 43
# name = 'Ghost'
p = Person()
print(getattr(p, "age")) # 43
print(p.age) # 43
# These two return - AttributeError: 'Person' object has no attribute 'name'
# print(getattr(p, 'name'))
# print(p.name)
# This will return the default value if name doesn't exist
print(getattr(p, 'name', 'No Name')) # No Name
# hasattr()
# -----------------------------------------------------------------------------
# returns true if an object has the given named attribute and false if not.
# hasattr(object, name)
class Person:
age = 23
name = 'Raja'
p = Person()
print('Person has age?:', hasattr(p, 'age')) # True
print('Person has salary?:', hasattr(p, 'salary')) # False
# id()
# -----------------------------------------------------------------------------
# returns the identity (unique integer) of an object
animal = 'cat'
def example1():
animal = 'dog'
print('local animal:', animal, id(animal))
def example2():
print('global animal:', animal, id(animal))
example1()
example2()
# local animal: dog 4316822976
# global animal: cat 4316589728
# vars()
# -----------------------------------------------------------------------------
# This one is maybe not so much an example of reflection but its useful and I
# keep forgetting about it so I'm going to include it here.
from pprint import pprint
pprint(vars())
# {'Person': <class '__main__.Person'>,
# 'Polygon': <class '__main__.Polygon'>,
# 'Triangle': <class '__main__.Triangle'>,
# '__annotations__': {},
# '__builtins__': <module 'builtins' (built-in)>,
# '__cached__': None,
# '__doc__': 'Reflection & Introspection',
# '__file__': 'reflection.py',
# '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x110266320>,
# '__name__': '__main__',
# '__package__': None,
# '__spec__': None,
# 'animal': 'cat',
# 'datetime': <module 'datetime' from '/usr/local/Cellar/python/3.6.4_4/
# Frameworks/Python.framework/Versions/3.6/lib/python3.6/datetime.py'>,
# 'example1': <function example1 at 0x110475e18>,
# 'example2': <function example2 at 0x1104757b8>,
# 'm': 'year',
# 'now': datetime.datetime(2018, 3, 27, 18, 34, 16, 366372),
# 'p': <__main__.Person object at 0x110462080>,
# 'pprint': <function pprint at 0x1104756a8>,
# 't': <__main__.Triangle object at 0x110266358>,
# 'test': <function test at 0x1101ebf28>,
# 'x': [],
# 'y': ['one'],
# 'z': None}
# self-document expressions
# -----------------------------------------------------------------------------
# As of Python 3.8, use f-strings and an = sign to self-document expressions.
# This can be really helpful for debugging. For example:
user1 = 'scott'
user2 = 'jessica'
print(f'{user1=} {user2=}')
# user1='scott' user2='jessica'