Skip to content

Commit d38105c

Browse files
committed
⬆️ Updated __init_subclass__.
This is a great way to do something without overriding metaclass.
1 parent 06061f3 commit d38105c

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Snippets
22

3-
Snippets is a repository that stores snippets from languages I want to work on.
3+
_Snippets is a repository that stores snippets from languages I want to work on, as well as some theories / thoughts that I have on the same topic._
44

55
[![Code Formatted](https://github.com/rentruewang/snippets/actions/workflows/format.yaml/badge.svg)](https://github.com/rentruewang/snippets/actions/workflows/format.yaml)
66

python/src/sub.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) 2024 RenChu Wang - All Rights Reserved
2+
3+
from typing import Any
4+
5+
6+
class Super1:
7+
@classmethod
8+
def __init_subclass__(cls, **kwargs: Any) -> None:
9+
print(cls, kwargs)
10+
11+
12+
class Sub1(Super1):
13+
pass
14+
15+
16+
class Sub2(Super1, param="hello", argument=123):
17+
pass
18+
19+
20+
class Super2:
21+
@classmethod
22+
def __init_subclass__(cls, required: str) -> None:
23+
print(cls, required)
24+
25+
26+
# Would fail:
27+
# TypeError: Super2.__init_subclass__() missing 1 required positional argument: 'required'
28+
# class Sub3(Super2):
29+
# pass
30+
31+
32+
class Sub4(Super2, required="yes"):
33+
pass
34+
35+
36+
if __name__ == "__main__":
37+
Sub1()
38+
Sub2()
39+
# Sub3()
40+
Sub4()

thoughts/fundamental.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Computation fundamentals
2+
3+
There are 2 main components in computing, data (measured by space complexity) and compute (measured by time complexity).
4+
5+
I theorize that
6+
Data = entities, storage, intrinsically exists
7+
Compute = mutation on data
8+
9+
Data is more fundamental than computation, because computation are performed on data (data = symbol, compute = instruction in Turning machine).
10+
11+
Think of the types of data as Markov, then a library / app is just moving on a path on this Markov state / graph. This state is exactly a Turing machine’s state as I can ask TM to simulate a function for me.
12+
13+
Even transmitting a packet counts as computation, because it's changing state (location) of a piece of data.
14+
15+
How do abstractions come into play?
16+
17+
- I think it's just types of data and the type of compute (abstract, reasoned with invariance) that can be performed on them. Considering that every function is data -> compute -> data, abstraction in terms of breaking down functions are just composing data into intermediate representation, and abstract interfaces are shared traits of different data + specialized compute for each kind of data.
18+
- Abstractions are just using computation to transform underlying data into the same mathematic model. Compiler optimizing this away = 0 cost. Some data structures might happen to have a better complexity but those are implementation details.

0 commit comments

Comments
 (0)