Skip to content

Commit 221ab20

Browse files
committedDec 6, 2021
fix toc
1 parent 61b14a7 commit 221ab20

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed
 

‎decorator.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* [Decorators that can receive parameters](#s1-1-3)
66
* [Decorators with first class functions/Closures](#s1-2)
77
* [First class functions/Closures in Python](#s1-2-1)
8-
* [Decorators by means of first class functions/closures](#s1-3)
9-
* [Decorators in the python standard library](#s1-4)
10-
* [@staticmethod and @classmethod](#s1-4-1)
11-
* [The functools library](#s1-4-2)
12-
* [dataclasses](#s1-4-3)
13-
* [contextlib](#s1-4-4)
8+
* [Decorators by means of first class functions/closures](#s1-2-2)
9+
* [Decorators in the python standard library](#s1-3)
10+
* [@staticmethod and @classmethod](#s1-3-1)
11+
* [The functools library](#s1-3-2)
12+
* [dataclasses](#s1-3-3)
13+
* [contextlib](#s1-3-4)
1414

1515

1616
# <a id='s1' />Python decorator walk-through
@@ -197,7 +197,7 @@ __Result:__
197197
>> type(say_miau) : <class '__main__.CountCalls'>
198198
>> say_miau.__name__ : say_miau
199199
>> say_miau.__doc__ : docstring: print the vocalization of a Felis Catus, also known as cat
200-
>> say_miau.__wrapped__ : <function say_miau at 0x7f85f26e7b80>
200+
>> say_miau.__wrapped__ : <function say_miau at 0x7faf0ede7b80>
201201
```
202202

203203
Attention!
@@ -350,7 +350,7 @@ for idx in range(1, 4):
350350
__Result:__
351351

352352
```
353-
>> LimitCalls function: <function square_me at 0x7f85f26f05e0> max_hits: 3 log_calls: False
353+
>> LimitCalls function: <function square_me at 0x7faf0edf05e0> max_hits: 3 log_calls: False
354354
>> square_me type: <class '__main__._LimitCalls'>
355355
>> idx: 1
356356
>> call # 1 returns: 4
@@ -380,7 +380,7 @@ __Result:__
380380

381381
```
382382
>> LimitCalls function: None max_hits: 4 log_calls: True
383-
>> wrapper function: <function cube_me at 0x7f85f26f0b80>
383+
>> wrapper function: <function cube_me at 0x7faf0edf0b80>
384384
```
385385

386386
cube\_me is a variable of type \_LimitCalls
@@ -457,7 +457,7 @@ __Result:__
457457
>> wrapper function: <class '__main__.Foo'>
458458
>> Calling: Foo #call: 1 positional-arguments: keyword-arguments:
459459
>> inside Foo.__init__
460-
>> Return from: Foo #call: 1 return-value: <__main__.Foo object at 0x7f85f26ed700>
460+
>> Return from: Foo #call: 1 return-value: <__main__.Foo object at 0x7faf0eded700>
461461
>> do_something in Foo
462462
```
463463

@@ -528,7 +528,7 @@ __Result:__
528528
There is a saying [Closures are the poor mans objects](https://stackoverflow.com/questions/2497801/closures-are-poor-mans-objects-and-vice-versa-what-does-this-mean), don't know who the poor man is, some languages, like Haskell, do without object systems at all. I think, it means that both objects and closures are equivalent means of storing state.
529529
Let's see, how this concept is put to use with decorators
530530

531-
## <a id='s1-3' />Decorators by means of first class functions/closures
531+
### <a id='s1-2-2' />Decorators by means of first class functions/closures
532532

533533
Time to examine other options. Python people like to do decorators with first class functions, that means lots of closures and functions returning closures/function values.
534534
In my book that is a bit of a brain damage, but let's go for it, real pythonistas are not afraid of brain damage! (i think that's quotable ;-))
@@ -627,8 +627,8 @@ for idx in range(1, 5):
627627
__Result:__
628628

629629
```
630-
>> LimitCalls2 _func: <function dec_three_from_me at 0x7f85f26f7a60> max_hits: 3 Log_calls: False
631-
>> LimitCalls in nested forward_func_call. func: <function dec_three_from_me at 0x7f85f26f7a60>
630+
>> LimitCalls2 _func: <function dec_three_from_me at 0x7faf0edf7a60> max_hits: 3 Log_calls: False
631+
>> LimitCalls in nested forward_func_call. func: <function dec_three_from_me at 0x7faf0edf7a60>
632632
>> type(dec_three_from_me) : <class 'function'>
633633
>> dec_three_from_me.__name__ : dec_three_from_me
634634
>> dec_three_from_me.__doc__ : None
@@ -672,7 +672,7 @@ __Result:__
672672

673673
```
674674
>> LimitCalls2 _func: None max_hits: 2 Log_calls: True
675-
>> LimitCalls in nested forward_func_call. func: <function dec_me at 0x7f85f26f90d0>
675+
>> LimitCalls in nested forward_func_call. func: <function dec_me at 0x7faf0edf90d0>
676676
>> idx: 1
677677
>> Calling: dec_me #call: 1 positional-arguments: 1 keyword-arguments:
678678
>> Return from: dec_me #call: 1 return-value: 0
@@ -713,7 +713,7 @@ __Result:__
713713
>> LimitCalls in nested forward_func_call. func: <class '__main__.Foo3'>
714714
>> Calling: Foo3 #call: 1 positional-arguments: keyword-arguments:
715715
>> inside Foo3.__init__
716-
>> Return from: Foo3 #call: 1 return-value: <__main__.Foo3 object at 0x7f85f26e4f10>
716+
>> Return from: Foo3 #call: 1 return-value: <__main__.Foo3 object at 0x7faf0ede4f10>
717717
>> do_something in Foo3
718718
```
719719

@@ -744,18 +744,18 @@ __Result:__
744744

745745
```
746746
>> LimitCalls2 _func: None max_hits: 3 Log_calls: True
747-
>> LimitCalls in nested forward_func_call. func: <function Foo4.do_something at 0x7f85f26f7dc0>
747+
>> LimitCalls in nested forward_func_call. func: <function Foo4.do_something at 0x7faf0edf7dc0>
748748
>> inside Foo4.__init__
749-
>> Calling: do_something #call: 1 positional-arguments: <__main__.Foo4 object at 0x7f85f2625af0> keyword-arguments:
749+
>> Calling: do_something #call: 1 positional-arguments: <__main__.Foo4 object at 0x7faf0ed25af0> keyword-arguments:
750750
>> do_something in Foo4
751751
>> Return from: do_something #call: 1 return-value: None
752752
```
753753

754754

755-
## <a id='s1-4' />Decorators in the python standard library
755+
## <a id='s1-3' />Decorators in the python standard library
756756

757757

758-
### <a id='s1-4-1' />@staticmethod and @classmethod
758+
### <a id='s1-3-1' />@staticmethod and @classmethod
759759

760760
@staticmethod and @classmethod are built-in decorators, you don't have to import any package in order to use them
761761

@@ -800,8 +800,8 @@ __Result:__
800800

801801
```
802802
>> absolute of a number: 3
803-
>> random number between 0 and 1 0.8809536053849658
804-
>> random number between 0 and 1 0.5903456526346241
803+
>> random number between 0 and 1 0.9720065130017221
804+
>> random number between 0 and 1 0.3578250940925185
805805
```
806806

807807
A method that is declared with the @classmthod decorator, here the first parameter is the class object. Note that a method like this doesn't have a self parameter.
@@ -839,7 +839,7 @@ print("color red: ", colour_red , "red:", colour_red.red , "green:", colour_red.
839839
__Result:__
840840

841841
```
842-
>> color red: <__main__.Colour object at 0x7f85f26f8fa0> red: 255 green: 0 blue: 0
842+
>> color red: <__main__.Colour object at 0x7faf0edf8fa0> red: 255 green: 0 blue: 0
843843
```
844844

845845
At first it doesn't make an awfull lot of sense, but lets derive the ColourWithAlphaChannel class from Colour.
@@ -873,7 +873,7 @@ print("color red: ", colour_red , "red:", colour_red.red , "green:", colour_red.
873873
__Result:__
874874

875875
```
876-
>> color red: <__main__.ColourWithAlphaChannel object at 0x7f85f26e6e50> red: 255 green: 0 blue: 0 alpha: 1.0
876+
>> color red: <__main__.ColourWithAlphaChannel object at 0x7faf0ede5e50> red: 255 green: 0 blue: 0 alpha: 1.0
877877
```
878878

879879
Other examples of alternate constructors in the standard library:
@@ -882,7 +882,7 @@ Other examples of alternate constructors in the standard library:
882882

883883

884884

885-
### <a id='s1-4-2' />The functools library
885+
### <a id='s1-3-2' />The functools library
886886

887887
The [functools library](https://docs.python.org/3/library/functools.html) comes as part of the python standard library.
888888
his library comes with some interesting decorators.
@@ -1086,11 +1086,11 @@ __Result:__
10861086
```
10871087

10881088

1089-
### <a id='s1-4-3' />dataclasses
1089+
### <a id='s1-3-3' />dataclasses
10901090

10911091
read all about it [here](https://docs.python.org/3/library/dataclasses.html)
10921092

1093-
### <a id='s1-4-4' />contextlib
1093+
### <a id='s1-3-4' />contextlib
10941094

10951095
read all about it [here](https://docs.python.org/3/reference/datamodel.html#context-managers)
10961096

‎decorator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def nested_function():
368368
Let's see, how this concept is put to use with decorators""")
369369

370370

371-
header_md("Decorators by means of first class functions/closures", nesting=2)
371+
header_md("Decorators by means of first class functions/closures", nesting=3)
372372

373373
print_md("""
374374
Time to examine other options. Python people like to do decorators with first class functions, that means lots of closures and functions returning closures/function values.

‎python-obj-system.md

+29-29
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ print("id(foo_obj) : ", id(foo_obj))
119119
__Result:__
120120

121121
```
122-
>> id(foo_obj) : 140503554112432
122+
>> id(foo_obj) : 140700527016880
123123
```
124124

125125
If two variables have the same object id value, then they both refer to the very same object/instance!
@@ -384,9 +384,9 @@ __Result:__
384384

385385
```
386386
>> *** mro in detail:
387-
>> class-in-mro: <class '\_\_main\_\_.Foo'> id: 140503550051248 cls.\_\_dict\_\_: {'\_\_module\_\_': '\_\_main\_\_', 'class\_var': 42, 'class\_var2': 43, '\_\_init\_\_': <function Foo.\_\_init\_\_ at 0x7fc9886e8790>, 'show\_derived': <function Foo.show\_derived at 0x7fc9886e8820>, 'make\_foo': <staticmethod object at 0x7fc9886ebdc0>, '\_\_doc\_\_': None}
388-
>> class-in-mro: <class '\_\_main\_\_.Base'> id: 140503550050304 cls.\_\_dict\_\_: {'\_\_module\_\_': '\_\_main\_\_', 'base\_class\_var': 'Base', '\_\_init\_\_': <function Base.\_\_init\_\_ at 0x7fc9886e85e0>, 'show\_base': <function Base.show\_base at 0x7fc9886e8670>, 'make\_base': <staticmethod object at 0x7fc9886ebfd0>, '\_\_dict\_\_': <attribute '\_\_dict\_\_' of 'Base' objects>, '\_\_weakref\_\_': <attribute '\_\_weakref\_\_' of 'Base' objects>, '\_\_doc\_\_': None}
389-
>> class-in-mro: <class 'object'> id: 4311403440 cls.\_\_dict\_\_: {'\_\_repr\_\_': <slot wrapper '\_\_repr\_\_' of 'object' objects>, '\_\_hash\_\_': <slot wrapper '\_\_hash\_\_' of 'object' objects>, '\_\_str\_\_': <slot wrapper '\_\_str\_\_' of 'object' objects>, '\_\_getattribute\_\_': <slot wrapper '\_\_getattribute\_\_' of 'object' objects>, '\_\_setattr\_\_': <slot wrapper '\_\_setattr\_\_' of 'object' objects>, '\_\_delattr\_\_': <slot wrapper '\_\_delattr\_\_' of 'object' objects>, '\_\_lt\_\_': <slot wrapper '\_\_lt\_\_' of 'object' objects>, '\_\_le\_\_': <slot wrapper '\_\_le\_\_' of 'object' objects>, '\_\_eq\_\_': <slot wrapper '\_\_eq\_\_' of 'object' objects>, '\_\_ne\_\_': <slot wrapper '\_\_ne\_\_' of 'object' objects>, '\_\_gt\_\_': <slot wrapper '\_\_gt\_\_' of 'object' objects>, '\_\_ge\_\_': <slot wrapper '\_\_ge\_\_' of 'object' objects>, '\_\_init\_\_': <slot wrapper '\_\_init\_\_' of 'object' objects>, '\_\_new\_\_': <built-in method \_\_new\_\_ of type object at 0x100facbb0>, '\_\_reduce\_ex\_\_': <method '\_\_reduce\_ex\_\_' of 'object' objects>, '\_\_reduce\_\_': <method '\_\_reduce\_\_' of 'object' objects>, '\_\_subclasshook\_\_': <method '\_\_subclasshook\_\_' of 'object' objects>, '\_\_init\_subclass\_\_': <method '\_\_init\_subclass\_\_' of 'object' objects>, '\_\_format\_\_': <method '\_\_format\_\_' of 'object' objects>, '\_\_sizeof\_\_': <method '\_\_sizeof\_\_' of 'object' objects>, '\_\_dir\_\_': <method '\_\_dir\_\_' of 'object' objects>, '\_\_class\_\_': <attribute '\_\_class\_\_' of 'object' objects>, '\_\_doc\_\_': 'The base class of the class hierarchy.\n\nWhen called, it accepts no arguments and returns a new featureless\ninstance that has no instance attributes and cannot be given any.\n'}
387+
>> class-in-mro: <class '\_\_main\_\_.Foo'> id: 140700524042176 cls.\_\_dict\_\_: {'\_\_module\_\_': '\_\_main\_\_', 'class\_var': 42, 'class\_var2': 43, '\_\_init\_\_': <function Foo.\_\_init\_\_ at 0x7ff764ee8790>, 'show\_derived': <function Foo.show\_derived at 0x7ff764ee8820>, 'make\_foo': <staticmethod object at 0x7ff764eebdc0>, '\_\_doc\_\_': None}
388+
>> class-in-mro: <class '\_\_main\_\_.Base'> id: 140700524041232 cls.\_\_dict\_\_: {'\_\_module\_\_': '\_\_main\_\_', 'base\_class\_var': 'Base', '\_\_init\_\_': <function Base.\_\_init\_\_ at 0x7ff764ee85e0>, 'show\_base': <function Base.show\_base at 0x7ff764ee8670>, 'make\_base': <staticmethod object at 0x7ff764eebfd0>, '\_\_dict\_\_': <attribute '\_\_dict\_\_' of 'Base' objects>, '\_\_weakref\_\_': <attribute '\_\_weakref\_\_' of 'Base' objects>, '\_\_doc\_\_': None}
389+
>> class-in-mro: <class 'object'> id: 4307454896 cls.\_\_dict\_\_: {'\_\_repr\_\_': <slot wrapper '\_\_repr\_\_' of 'object' objects>, '\_\_hash\_\_': <slot wrapper '\_\_hash\_\_' of 'object' objects>, '\_\_str\_\_': <slot wrapper '\_\_str\_\_' of 'object' objects>, '\_\_getattribute\_\_': <slot wrapper '\_\_getattribute\_\_' of 'object' objects>, '\_\_setattr\_\_': <slot wrapper '\_\_setattr\_\_' of 'object' objects>, '\_\_delattr\_\_': <slot wrapper '\_\_delattr\_\_' of 'object' objects>, '\_\_lt\_\_': <slot wrapper '\_\_lt\_\_' of 'object' objects>, '\_\_le\_\_': <slot wrapper '\_\_le\_\_' of 'object' objects>, '\_\_eq\_\_': <slot wrapper '\_\_eq\_\_' of 'object' objects>, '\_\_ne\_\_': <slot wrapper '\_\_ne\_\_' of 'object' objects>, '\_\_gt\_\_': <slot wrapper '\_\_gt\_\_' of 'object' objects>, '\_\_ge\_\_': <slot wrapper '\_\_ge\_\_' of 'object' objects>, '\_\_init\_\_': <slot wrapper '\_\_init\_\_' of 'object' objects>, '\_\_new\_\_': <built-in method \_\_new\_\_ of type object at 0x100be8bb0>, '\_\_reduce\_ex\_\_': <method '\_\_reduce\_ex\_\_' of 'object' objects>, '\_\_reduce\_\_': <method '\_\_reduce\_\_' of 'object' objects>, '\_\_subclasshook\_\_': <method '\_\_subclasshook\_\_' of 'object' objects>, '\_\_init\_subclass\_\_': <method '\_\_init\_subclass\_\_' of 'object' objects>, '\_\_format\_\_': <method '\_\_format\_\_' of 'object' objects>, '\_\_sizeof\_\_': <method '\_\_sizeof\_\_' of 'object' objects>, '\_\_dir\_\_': <method '\_\_dir\_\_' of 'object' objects>, '\_\_class\_\_': <attribute '\_\_class\_\_' of 'object' objects>, '\_\_doc\_\_': 'The base class of the class hierarchy.\n\nWhen called, it accepts no arguments and returns a new featureless\ninstance that has no instance attributes and cannot be given any.\n'}
390390
>> *** eof mro in detail
391391
```
392392

@@ -402,7 +402,7 @@ print("foo_obj.__class__.__dict__ : ", foo_obj.__class__.__dict__)
402402
__Result:__
403403

404404
```
405-
>> foo_obj.__class__.__dict__ : {'__module__': '__main__', 'class_var': 42, 'class_var2': 43, '__init__': <function Foo.__init__ at 0x7fc9886e8790>, 'show_derived': <function Foo.show_derived at 0x7fc9886e8820>, 'make_foo': <staticmethod object at 0x7fc9886ebdc0>, '__doc__': None}
405+
>> foo_obj.__class__.__dict__ : {'__module__': '__main__', 'class_var': 42, 'class_var2': 43, '__init__': <function Foo.__init__ at 0x7ff764ee8790>, 'show_derived': <function Foo.show_derived at 0x7ff764ee8820>, 'make_foo': <staticmethod object at 0x7ff764eebdc0>, '__doc__': None}
406406
```
407407

408408
Again, the [dir](https://docs.python.org/3/library/functions.html#dir) built-in function does different things, depending on the argument type
@@ -453,7 +453,7 @@ print("inspect.getmembers(foo_obj): ", inspect.getmembers(foo_obj))
453453
__Result:__
454454

455455
```
456-
>> inspect.getmembers(foo_obj): [('__class__', <class '__main__.Foo'>), ('__delattr__', <method-wrapper '__delattr__' of Foo object at 0x7fc9886eb2b0>), ('__dict__', {'obj_var_base': 10, 'obj_var_a': 42, 'obj_var_b': 'name'}), ('__dir__', <built-in method __dir__ of Foo object at 0x7fc9886eb2b0>), ('__doc__', None), ('__eq__', <method-wrapper '__eq__' of Foo object at 0x7fc9886eb2b0>), ('__format__', <built-in method __format__ of Foo object at 0x7fc9886eb2b0>), ('__ge__', <method-wrapper '__ge__' of Foo object at 0x7fc9886eb2b0>), ('__getattribute__', <method-wrapper '__getattribute__' of Foo object at 0x7fc9886eb2b0>), ('__gt__', <method-wrapper '__gt__' of Foo object at 0x7fc9886eb2b0>), ('__hash__', <method-wrapper '__hash__' of Foo object at 0x7fc9886eb2b0>), ('__init__', <bound method Foo.__init__ of <__main__.Foo object at 0x7fc9886eb2b0>>), ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x7fc98830c3b0>), ('__le__', <method-wrapper '__le__' of Foo object at 0x7fc9886eb2b0>), ('__lt__', <method-wrapper '__lt__' of Foo object at 0x7fc9886eb2b0>), ('__module__', '__main__'), ('__ne__', <method-wrapper '__ne__' of Foo object at 0x7fc9886eb2b0>), ('__new__', <built-in method __new__ of type object at 0x100facbb0>), ('__reduce__', <built-in method __reduce__ of Foo object at 0x7fc9886eb2b0>), ('__reduce_ex__', <built-in method __reduce_ex__ of Foo object at 0x7fc9886eb2b0>), ('__repr__', <method-wrapper '__repr__' of Foo object at 0x7fc9886eb2b0>), ('__setattr__', <method-wrapper '__setattr__' of Foo object at 0x7fc9886eb2b0>), ('__sizeof__', <built-in method __sizeof__ of Foo object at 0x7fc9886eb2b0>), ('__str__', <method-wrapper '__str__' of Foo object at 0x7fc9886eb2b0>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x7fc98830c3b0>), ('__weakref__', None), ('base_class_var', 'Base'), ('class_var', 42), ('class_var2', 43), ('make_base', <function Base.make_base at 0x7fc9886e8700>), ('make_foo', <function Foo.make_foo at 0x7fc9886e88b0>), ('obj_var_a', 42), ('obj_var_b', 'name'), ('obj_var_base', 10), ('show_base', <bound method Base.show_base of <__main__.Foo object at 0x7fc9886eb2b0>>), ('show_derived', <bound method Foo.show_derived of <__main__.Foo object at 0x7fc9886eb2b0>>)]
456+
>> inspect.getmembers(foo_obj): [('__class__', <class '__main__.Foo'>), ('__delattr__', <method-wrapper '__delattr__' of Foo object at 0x7ff764eeb2b0>), ('__dict__', {'obj_var_base': 10, 'obj_var_a': 42, 'obj_var_b': 'name'}), ('__dir__', <built-in method __dir__ of Foo object at 0x7ff764eeb2b0>), ('__doc__', None), ('__eq__', <method-wrapper '__eq__' of Foo object at 0x7ff764eeb2b0>), ('__format__', <built-in method __format__ of Foo object at 0x7ff764eeb2b0>), ('__ge__', <method-wrapper '__ge__' of Foo object at 0x7ff764eeb2b0>), ('__getattribute__', <method-wrapper '__getattribute__' of Foo object at 0x7ff764eeb2b0>), ('__gt__', <method-wrapper '__gt__' of Foo object at 0x7ff764eeb2b0>), ('__hash__', <method-wrapper '__hash__' of Foo object at 0x7ff764eeb2b0>), ('__init__', <bound method Foo.__init__ of <__main__.Foo object at 0x7ff764eeb2b0>>), ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x7ff764c157c0>), ('__le__', <method-wrapper '__le__' of Foo object at 0x7ff764eeb2b0>), ('__lt__', <method-wrapper '__lt__' of Foo object at 0x7ff764eeb2b0>), ('__module__', '__main__'), ('__ne__', <method-wrapper '__ne__' of Foo object at 0x7ff764eeb2b0>), ('__new__', <built-in method __new__ of type object at 0x100be8bb0>), ('__reduce__', <built-in method __reduce__ of Foo object at 0x7ff764eeb2b0>), ('__reduce_ex__', <built-in method __reduce_ex__ of Foo object at 0x7ff764eeb2b0>), ('__repr__', <method-wrapper '__repr__' of Foo object at 0x7ff764eeb2b0>), ('__setattr__', <method-wrapper '__setattr__' of Foo object at 0x7ff764eeb2b0>), ('__sizeof__', <built-in method __sizeof__ of Foo object at 0x7ff764eeb2b0>), ('__str__', <method-wrapper '__str__' of Foo object at 0x7ff764eeb2b0>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x7ff764c157c0>), ('__weakref__', None), ('base_class_var', 'Base'), ('class_var', 42), ('class_var2', 43), ('make_base', <function Base.make_base at 0x7ff764ee8700>), ('make_foo', <function Foo.make_foo at 0x7ff764ee88b0>), ('obj_var_a', 42), ('obj_var_b', 'name'), ('obj_var_base', 10), ('show_base', <bound method Base.show_base of <__main__.Foo object at 0x7ff764eeb2b0>>), ('show_derived', <bound method Foo.show_derived of <__main__.Foo object at 0x7ff764eeb2b0>>)]
457457
```
458458

459459
Attention!
@@ -489,7 +489,7 @@ print("id(foo_obj) : ", id(foo_obj), " str(foo_obj) : ", str(foo_obj))
489489
__Result:__
490490

491491
```
492-
>> id(foo_obj) : 140503554110128 str(foo_obj) : <__main__.Foo object at 0x7fc9886eb2b0>
492+
>> id(foo_obj) : 140700527014576 str(foo_obj) : <__main__.Foo object at 0x7ff764eeb2b0>
493493
```
494494

495495
The following expressions refer to the same thing: the type of the object foo\_obj, also known as the class of foo\_obj
@@ -511,9 +511,9 @@ assert id(type(foo_obj)) == id(foo_obj.__class__)
511511
__Result:__
512512

513513
```
514-
>> type(foo_obj) : <class '__main__.Foo'> id(type(foo_obj)) : 140503550051248 type(foo_obj).__name__ : Foo
515-
>> str(foo_obj.__class__) : <class '__main__.Foo'> id(foo_obj.__class__) : 140503550051248 foo_obj.__class__.__name__ : Foo
516-
>> str(Foo) : <class '__main__.Foo'> id(Foo) : 140503550051248 Foo.__name__ : Foo
514+
>> type(foo_obj) : <class '__main__.Foo'> id(type(foo_obj)) : 140700524042176 type(foo_obj).__name__ : Foo
515+
>> str(foo_obj.__class__) : <class '__main__.Foo'> id(foo_obj.__class__) : 140700524042176 foo_obj.__class__.__name__ : Foo
516+
>> str(Foo) : <class '__main__.Foo'> id(Foo) : 140700524042176 Foo.__name__ : Foo
517517
```
518518

519519
The Foo class members
@@ -533,8 +533,8 @@ print("dir(foo_obj.__class__) :", dir( foo_obj.__class__))
533533
__Result:__
534534

535535
```
536-
>> foo_obj.__class__.__dict__ : {'__module__': '__main__', 'class_var': 42, 'class_var2': 43, '__init__': <function Foo.__init__ at 0x7fc9886e8790>, 'show_derived': <function Foo.show_derived at 0x7fc9886e8820>, 'make_foo': <staticmethod object at 0x7fc9886ebdc0>, '__doc__': None}
537-
>> Foo.__dict__ : {'__module__': '__main__', 'class_var': 42, 'class_var2': 43, '__init__': <function Foo.__init__ at 0x7fc9886e8790>, 'show_derived': <function Foo.show_derived at 0x7fc9886e8820>, 'make_foo': <staticmethod object at 0x7fc9886ebdc0>, '__doc__': None}
536+
>> foo_obj.__class__.__dict__ : {'__module__': '__main__', 'class_var': 42, 'class_var2': 43, '__init__': <function Foo.__init__ at 0x7ff764ee8790>, 'show_derived': <function Foo.show_derived at 0x7ff764ee8820>, 'make_foo': <staticmethod object at 0x7ff764eebdc0>, '__doc__': None}
537+
>> Foo.__dict__ : {'__module__': '__main__', 'class_var': 42, 'class_var2': 43, '__init__': <function Foo.__init__ at 0x7ff764ee8790>, 'show_derived': <function Foo.show_derived at 0x7ff764ee8820>, 'make_foo': <staticmethod object at 0x7ff764eebdc0>, '__doc__': None}
538538
>> dir(foo_obj.__class__) : ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'base_class_var', 'class_var', 'class_var2', 'make_base', 'make_foo', 'show_base', 'show_derived']
539539
```
540540

@@ -558,10 +558,10 @@ assert type(Foo.__class__) == type(Foo.__class__.__class__)
558558
__Result:__
559559

560560
```
561-
>> type(foo_obj.__class__.__class__): <class 'type'> id( foo_obj.__class__.__class__ ) : 4311403848 foo_obj.__class__.__class__.__name__ : type
562-
>> type(Foo) : <class 'type'> id(type(Foo)) : 4311403848 Foo.__class__.__name__ : type
563-
>> type(Foo.__class__) : <class 'type'> id(type(Foo.__class__)) : 4311403848 Foo.__class__.__name__ : type
564-
>> type(Foo.__class__.__class__) : <class 'type'> id(type(Foo.__class__.__class__)) : 4311403848
561+
>> type(foo_obj.__class__.__class__): <class 'type'> id( foo_obj.__class__.__class__ ) : 4307455304 foo_obj.__class__.__class__.__name__ : type
562+
>> type(Foo) : <class 'type'> id(type(Foo)) : 4307455304 Foo.__class__.__name__ : type
563+
>> type(Foo.__class__) : <class 'type'> id(type(Foo.__class__)) : 4307455304 Foo.__class__.__name__ : type
564+
>> type(Foo.__class__.__class__) : <class 'type'> id(type(Foo.__class__.__class__)) : 4307455304
565565
```
566566

567567
The type of the type is the metaclass - the metaclass constructs the Class object! (the class of an object is also an object!)
@@ -596,7 +596,7 @@ print(" everything accessible form metaclass: dir( foo_obj.__class__.__class__ )
596596
__Result:__
597597

598598
```
599-
>> metaclass members: foo_obj.__class__.__class__.__dict__ : {'__repr__': <slot wrapper '__repr__' of 'type' objects>, '__call__': <slot wrapper '__call__' of 'type' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'type' objects>, '__setattr__': <slot wrapper '__setattr__' of 'type' objects>, '__delattr__': <slot wrapper '__delattr__' of 'type' objects>, '__init__': <slot wrapper '__init__' of 'type' objects>, '__new__': <built-in method __new__ of type object at 0x100facd48>, 'mro': <method 'mro' of 'type' objects>, '__subclasses__': <method '__subclasses__' of 'type' objects>, '__prepare__': <method '__prepare__' of 'type' objects>, '__instancecheck__': <method '__instancecheck__' of 'type' objects>, '__subclasscheck__': <method '__subclasscheck__' of 'type' objects>, '__dir__': <method '__dir__' of 'type' objects>, '__sizeof__': <method '__sizeof__' of 'type' objects>, '__basicsize__': <member '__basicsize__' of 'type' objects>, '__itemsize__': <member '__itemsize__' of 'type' objects>, '__flags__': <member '__flags__' of 'type' objects>, '__weakrefoffset__': <member '__weakrefoffset__' of 'type' objects>, '__base__': <member '__base__' of 'type' objects>, '__dictoffset__': <member '__dictoffset__' of 'type' objects>, '__mro__': <member '__mro__' of 'type' objects>, '__name__': <attribute '__name__' of 'type' objects>, '__qualname__': <attribute '__qualname__' of 'type' objects>, '__bases__': <attribute '__bases__' of 'type' objects>, '__module__': <attribute '__module__' of 'type' objects>, '__abstractmethods__': <attribute '__abstractmethods__' of 'type' objects>, '__dict__': <attribute '__dict__' of 'type' objects>, '__doc__': <attribute '__doc__' of 'type' objects>, '__text_signature__': <attribute '__text_signature__' of 'type' objects>}
599+
>> metaclass members: foo_obj.__class__.__class__.__dict__ : {'__repr__': <slot wrapper '__repr__' of 'type' objects>, '__call__': <slot wrapper '__call__' of 'type' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'type' objects>, '__setattr__': <slot wrapper '__setattr__' of 'type' objects>, '__delattr__': <slot wrapper '__delattr__' of 'type' objects>, '__init__': <slot wrapper '__init__' of 'type' objects>, '__new__': <built-in method __new__ of type object at 0x100be8d48>, 'mro': <method 'mro' of 'type' objects>, '__subclasses__': <method '__subclasses__' of 'type' objects>, '__prepare__': <method '__prepare__' of 'type' objects>, '__instancecheck__': <method '__instancecheck__' of 'type' objects>, '__subclasscheck__': <method '__subclasscheck__' of 'type' objects>, '__dir__': <method '__dir__' of 'type' objects>, '__sizeof__': <method '__sizeof__' of 'type' objects>, '__basicsize__': <member '__basicsize__' of 'type' objects>, '__itemsize__': <member '__itemsize__' of 'type' objects>, '__flags__': <member '__flags__' of 'type' objects>, '__weakrefoffset__': <member '__weakrefoffset__' of 'type' objects>, '__base__': <member '__base__' of 'type' objects>, '__dictoffset__': <member '__dictoffset__' of 'type' objects>, '__mro__': <member '__mro__' of 'type' objects>, '__name__': <attribute '__name__' of 'type' objects>, '__qualname__': <attribute '__qualname__' of 'type' objects>, '__bases__': <attribute '__bases__' of 'type' objects>, '__module__': <attribute '__module__' of 'type' objects>, '__abstractmethods__': <attribute '__abstractmethods__' of 'type' objects>, '__dict__': <attribute '__dict__' of 'type' objects>, '__doc__': <attribute '__doc__' of 'type' objects>, '__text_signature__': <attribute '__text_signature__' of 'type' objects>}
600600
>> everything accessible form metaclass: dir( foo_obj.__class__.__class__ ) : ['__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__delattr__', '__dict__', '__dictoffset__', '__dir__', '__doc__', '__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__instancecheck__', '__itemsize__', '__le__', '__lt__', '__module__', '__mro__', '__name__', '__ne__', '__new__', '__prepare__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasscheck__', '__subclasses__', '__subclasshook__', '__text_signature__', '__weakrefoffset__', 'mro']
601601
```
602602

@@ -658,7 +658,7 @@ __Result:__
658658
```
659659
>> calling Base.__init__
660660
>> calling Foo.__init__
661-
>> foo_obj : <__main__.Foo object at 0x7fc9886ea5e0>
661+
>> foo_obj : <__main__.Foo object at 0x7ff764eea5e0>
662662
>> foo_obj.__dict__ : {'obj_var_base': 10, 'obj_var_a': 42, 'obj_var_b': 'name'}
663663
```
664664

@@ -681,7 +681,7 @@ __Result:__
681681
```
682682
>> calling Base.__init__
683683
>> calling Foo.__init__
684-
>> instance_of_foo : <__main__.Foo object at 0x7fc9886ea400>
684+
>> instance_of_foo : <__main__.Foo object at 0x7ff764eea400>
685685
>> instance_of_foo.__dict__ : {'obj_var_base': 10, 'obj_var_a': 42, 'obj_var_b': 'name'}
686686
```
687687

@@ -768,14 +768,14 @@ assert id(sqrt_root_two_a) == id(sqrt_root_two_b)
768768
__Result:__
769769

770770
```
771-
>> Singleton_metaclass: __new__ meta_class: <class '__main__.Singleton_metaclass'> name: SquareRootOfTwo bases: () cls_dict: {'__module__': '__main__', '__qualname__': 'SquareRootOfTwo', '__init__': <function SquareRootOfTwo.__init__ at 0x7fc9886f13a0>} kwargs: {}
771+
>> Singleton_metaclass: __new__ meta_class: <class '__main__.Singleton_metaclass'> name: SquareRootOfTwo bases: () cls_dict: {'__module__': '__main__', '__qualname__': 'SquareRootOfTwo', '__init__': <function SquareRootOfTwo.__init__ at 0x7ff764efc3a0>} kwargs: {}
772772
>> Singleton_metaclass: __new__ return value: <class '__main__.SquareRootOfTwo'> type(class_instance): <class '__main__.Singleton_metaclass'>
773773
>> creating the objects instances...
774774
>> Singleton_metaclass: __call__ args: kwargs: {}
775-
>> SquareRootOfTwo.__init__ self: <__main__.SquareRootOfTwo object at 0x7fc9886fdf70>
776-
>> sqrt_two_a id(sqrt_root_two_a): 140503554187120 type(sqrt_root_two_a): <class '__main__.SquareRootOfTwo'> sqrt_root_two_a.value: 1.4142135623730951
775+
>> SquareRootOfTwo.__init__ self: <__main__.SquareRootOfTwo object at 0x7ff764efef70>
776+
>> sqrt_two_a id(sqrt_root_two_a): 140700527095664 type(sqrt_root_two_a): <class '__main__.SquareRootOfTwo'> sqrt_root_two_a.value: 1.4142135623730951
777777
>> Singleton_metaclass: __call__ args: kwargs: {}
778-
>> sqrt_two_b id(sqrt_root_two_b) 140503554187120 type(sqrt_root_two_b): <class '__main__.SquareRootOfTwo'> sqrt_root_two_b.value: 1.4142135623730951
778+
>> sqrt_two_b id(sqrt_root_two_b) 140700527095664 type(sqrt_root_two_b): <class '__main__.SquareRootOfTwo'> sqrt_root_two_b.value: 1.4142135623730951
779779
```
780780

781781

@@ -879,19 +879,19 @@ assert id(sqrt_root_three_a) == id(sqrt_root_three_b)
879879
__Result:__
880880

881881
```
882-
>> Singleton_metaclass_with_args: __new__ meta_class: <class '__main__.Singleton_metaclass_with_args'> name: SquareRootOfTwo bases: (<class '__main__.AnySquareRoot'>,) cls_dict: {'__module__': '__main__', '__qualname__': 'SquareRootOfTwo', '__init__': <function SquareRootOfTwo.__init__ at 0x7fc9886f1a60>, '__classcell__': <cell at 0x7fc9886fef70: empty>} kwargs: {'arg_num': 2}
882+
>> Singleton_metaclass_with_args: __new__ meta_class: <class '__main__.Singleton_metaclass_with_args'> name: SquareRootOfTwo bases: (<class '__main__.AnySquareRoot'>,) cls_dict: {'__module__': '__main__', '__qualname__': 'SquareRootOfTwo', '__init__': <function SquareRootOfTwo.__init__ at 0x7ff764efca60>, '__classcell__': <cell at 0x7ff764efdf70: empty>} kwargs: {'arg_num': 2}
883883
>> Singleton_metaclass_with_args: __new__ return value: <class '__main__.SquareRootOfTwo'> type(class_instance): <class '__main__.Singleton_metaclass_with_args'>
884-
>> Singleton_metaclass_with_args: __new__ meta_class: <class '__main__.Singleton_metaclass_with_args'> name: SquareRootOfThree bases: (<class '__main__.AnySquareRoot'>,) cls_dict: {'__module__': '__main__', '__qualname__': 'SquareRootOfThree', '__init__': <function SquareRootOfThree.__init__ at 0x7fc9886f1af0>, '__classcell__': <cell at 0x7fc9886fedf0: empty>} kwargs: {'arg_num': 3}
884+
>> Singleton_metaclass_with_args: __new__ meta_class: <class '__main__.Singleton_metaclass_with_args'> name: SquareRootOfThree bases: (<class '__main__.AnySquareRoot'>,) cls_dict: {'__module__': '__main__', '__qualname__': 'SquareRootOfThree', '__init__': <function SquareRootOfThree.__init__ at 0x7ff764efcaf0>, '__classcell__': <cell at 0x7ff764efddf0: empty>} kwargs: {'arg_num': 3}
885885
>> Singleton_metaclass_with_args: __new__ return value: <class '__main__.SquareRootOfThree'> type(class_instance): <class '__main__.Singleton_metaclass_with_args'>
886886
>> creating the objects instances...
887887
>> Singleton_metaclass_with_args: __call__ args: kwargs: {}
888-
>> sqrt_two_a id(sqrt_root_two_a): 140503554190736 type(sqrt_root_two_a): <class '__main__.SquareRootOfTwo'> sqrt_root_two_a.value: 1.4142135623730951
888+
>> sqrt_two_a id(sqrt_root_two_a): 140700527091088 type(sqrt_root_two_a): <class '__main__.SquareRootOfTwo'> sqrt_root_two_a.value: 1.4142135623730951
889889
>> Singleton_metaclass_with_args: __call__ args: kwargs: {}
890-
>> sqrt_two_b id(sqrt_root_two_b) 140503554190736 type(sqrt_root_two_b): <class '__main__.SquareRootOfTwo'> sqrt_root_two_b.value: 1.4142135623730951
890+
>> sqrt_two_b id(sqrt_root_two_b) 140700527091088 type(sqrt_root_two_b): <class '__main__.SquareRootOfTwo'> sqrt_root_two_b.value: 1.4142135623730951
891891
>> Singleton_metaclass_with_args: __call__ args: kwargs: {}
892-
>> sqrt_three_a id(sqrt_root_three_a): 140503554190592 type(sqrt_root_three_a): <class '__main__.SquareRootOfThree'> sqrt_root_three_a.value: 1.7320508075688772
892+
>> sqrt_three_a id(sqrt_root_three_a): 140700527090944 type(sqrt_root_three_a): <class '__main__.SquareRootOfThree'> sqrt_root_three_a.value: 1.7320508075688772
893893
>> Singleton_metaclass_with_args: __call__ args: kwargs: {}
894-
>> sqrt_three_b id(sqrt_root_three_b) 140503554190592 type(sqrt_root_three_b): <class '__main__.SquareRootOfThree'> sqrt_root_three_b.value: 1.7320508075688772
894+
>> sqrt_three_b id(sqrt_root_three_b) 140700527090944 type(sqrt_root_three_b): <class '__main__.SquareRootOfThree'> sqrt_root_three_b.value: 1.7320508075688772
895895
```
896896

897897

0 commit comments

Comments
 (0)
Please sign in to comment.