Skip to content

Commit 4d56783

Browse files
Merge pull request #12 from praisetompane/container_abcs
docs: abstract base classes and container abstract base classes
2 parents eef2e10 + 0eb7f4d commit 4d56783

17 files changed

Lines changed: 179 additions & 5 deletions

File tree

.spellcheck_exceptions_dictionary.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ userspace
5252
userguide
5353
sdist
5454
exe
55-
55+
hashable
56+
checksubclass
57+
issubclasshook
58+
issubclass
59+
metaclass
5660

5761
# python keywords
5862
pprint

2_standard_library/29_python_runtime_services/0_def.txt renamed to 2_standard_library/29_python_runtime_services/0_python_runtime_services.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ def python_runtime_services:
1515

1616
- proof: ???
1717

18-
References: ???
18+
References:
19+
The Python Standard Library. Python Runtime Services. 2025. https://docs.python.org/3/library/python.html
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def abcs:
2+
- formal: ???
3+
4+
- in words: ???
5+
6+
- plain english: ???
7+
8+
- intuition: ???
9+
10+
- properties:
11+
- specification:
12+
- https://peps.python.org/pep-3119/
13+
- https://docs.python.org/3/library/abc.html#module-abc
14+
15+
- implementation: https://github.com/python/cpython/blob/3.13/Lib/abc.py
16+
17+
18+
- examples: ???
19+
20+
- use cases:
21+
- register a third-party class as a subclass of our own abstract class(i.e. metaclass)
22+
this enables using the third-party class in polymorphic code as a subclass.
23+
24+
- proof: ???
25+
26+
References:
27+
The Python Standard Library. Python Runtime Services. 2025. https://docs.python.org/3/library/python.html

2_standard_library/4_built_in_types/5_iterator_types/1_iterator.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ def iterator:
2626
- proof: None. It is a definition.
2727

2828
References:
29-
https://docs.python.org/3.11/glossary.html#term-iterator
29+
The Python Standard Library. 2025. https://docs.python.org/3/glossary.html#term-iterator
3030

2_standard_library/4_built_in_types/5_iterator_types/2_generator.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ def generator | generator function:
2121
- proof: None. It is a definition.
2222

2323
References:
24-
https://docs.python.org/3.11/glossary.html#term-generator
24+
The Python Standard Library. 2025. https://docs.python.org/3/glossary.html#term-generator
2525

2626

2_standard_library/4_built_in_types/5_iterator_types/3_generator_expression.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
- proof: None. It is a definition.
2020

2121
References:
22-
https://docs.python.org/3.11/glossary.html#term-generator-iterator
22+
The Python Standard Library. 2025. https://docs.python.org/3/glossary.html#term-generator-iterator
2323

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def collections_abcs:
2+
- formal: ???
3+
4+
- in words: ???
5+
6+
- plain english: ???
7+
8+
- intuition: ???
9+
10+
- properties:
11+
- implementation: https://github.com/python/cpython/tree/3.11/Lib/_collections_abc.py
12+
- specification: https://docs.python.org/3/library/collections.abc.html
13+
- examples:
14+
- test interface compliance:
15+
- hashable
16+
- iterable
17+
- mapping
18+
- ...
19+
20+
- use cases:
21+
- implement custom classes that satisfy the container API.
22+
- test compliance to a collections' interface using `issubclass` or `isinstance`.
23+
24+
- proof: ???
25+
26+
References:
27+
The Python Standard Library. 2025. collections.abc — Abstract Base Classes for Containers, Data Types. https://docs.python.org/3/library/collections.abc.html
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from workers.worker import Worker
2+
from thirdparty.third_party_class import ThirdPartyWorker
3+
from interfaces.s_one import SOneContract
4+
from interfaces.s_two import STwoContract
5+
6+
if __name__ == "__main__":
7+
8+
"""
9+
This is the registration of the third party to enable our system to treat it as a subclass of SOneContract".
10+
This makes ThirdPartyWorker a virtual subclass of SOneContract
11+
"""
12+
SOneContract.register(ThirdPartyWorker)
13+
14+
workers = [Worker(), ThirdPartyWorker()]
15+
16+
for worker in workers:
17+
if(isinstance(worker, SOneContract)):
18+
worker.do_work()
19+
if(isinstance(worker, STwoContract)):
20+
worker.s_two_work()

4_experiments/2_standard_library/29_python_runtime_services/9_abcs/1_abc_meta/interfaces/__init__.py

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
class SOneContract(metaclass = ABCMeta):
4+
5+
@abstractmethod
6+
def do_work(self):
7+
raise NotImplemented
8+
9+
"""
10+
# NB: @abstractmethod only applies to subclasses created using regular inheritance.
11+
virtual sub classes can be instantiated without implementing it, while regular subclasses cannot.
12+
@abstractmethod
13+
def s_one_specialty(self):
14+
raise NotImplemented
15+
"""
16+
17+
def __issubclasshook__(cls):
18+
"""
19+
NB: Alternativeltm we overload the behvaiour of `issubclass` function for SOneContract
20+
and define what it means to be a SOneContract.
21+
"""
22+
pass

0 commit comments

Comments
 (0)