forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecial_attrs.py
44 lines (26 loc) · 996 Bytes
/
special_attrs.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
'''
4.13. Special Attributes
The implementation adds a few special read-only attributes to
several object types, where they are relevant.
Some of these are not reported by the dir() built-in function.
https://docs.python.org/3/library/stdtypes.html#special-attributes
'''
obj_attrs = {'__dict__', '__class__'}
cls_data_attrs = {'__slots__', '__bases__', '__name__', '__qualname__', '__mro__'}
cls_methods = {'mro', '__subclasses__'}
cls_attrs = cls_data_attrs | cls_methods
an_object = object()
class EmptyClass():
pass
an_instance = EmptyClass()
class EmptySlots():
__slots__ = ()
a_slots_instance = EmptySlots()
objs = EmptyClass, EmptySlots, an_object, an_instance, a_slots_instance
for obj in objs:
print('-' * 60)
print(repr(obj), ':', type(obj))
dir_obj = set(dir(obj))
print('obj_attrs not listed:', sorted(obj_attrs - dir_obj))
print('all cls_attrs :', sorted(cls_attrs))
print('cls_attrs not listed:', sorted(cls_attrs - dir_obj))