Skip to content

Commit

Permalink
⬆️ Updated __init_subclass__.
Browse files Browse the repository at this point in the history
This is a great way to do something without overriding metaclass.
  • Loading branch information
rentruewang committed Oct 7, 2024
1 parent 06061f3 commit d38105c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Snippets

Snippets is a repository that stores snippets from languages I want to work on.
_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._

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

Expand Down
40 changes: 40 additions & 0 deletions python/src/sub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2024 RenChu Wang - All Rights Reserved

from typing import Any


class Super1:
@classmethod
def __init_subclass__(cls, **kwargs: Any) -> None:
print(cls, kwargs)


class Sub1(Super1):
pass


class Sub2(Super1, param="hello", argument=123):
pass


class Super2:
@classmethod
def __init_subclass__(cls, required: str) -> None:
print(cls, required)


# Would fail:
# TypeError: Super2.__init_subclass__() missing 1 required positional argument: 'required'
# class Sub3(Super2):
# pass


class Sub4(Super2, required="yes"):
pass


if __name__ == "__main__":
Sub1()
Sub2()
# Sub3()
Sub4()
18 changes: 18 additions & 0 deletions thoughts/fundamental.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Computation fundamentals

There are 2 main components in computing, data (measured by space complexity) and compute (measured by time complexity).

I theorize that
Data = entities, storage, intrinsically exists
Compute = mutation on data

Data is more fundamental than computation, because computation are performed on data (data = symbol, compute = instruction in Turning machine).

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.

Even transmitting a packet counts as computation, because it's changing state (location) of a piece of data.

How do abstractions come into play?

- 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.
- 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 comments on commit d38105c

Please sign in to comment.