Skip to content

Commit d193ed3

Browse files
committed
πŸ§‘β€πŸ« Experiments on dataclasses.
Python's `dataclass` uses type hints to determine if a field is a member or class variable, and python's classes uses `builtin_function_or_method` vs `types.FunctionType` to tell if a `Callable` would be a method would be bound to the class.
1 parent d98fd0e commit d193ed3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

β€Žpython/src/data-classes.pyβ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) RenChu Wang - All Rights Reserved
22

33
import dataclasses as dcls
4+
from collections.abc import Callable
45

56

67
@dcls.dataclass(frozen=True)
@@ -17,10 +18,24 @@ class Sub2(Base):
1718
sub: str
1819

1920

21+
@dcls.dataclass(frozen=True)
22+
class HasMethods:
23+
field: int = 3
24+
25+
e = lambda self: print(self)
26+
f = print
27+
g: Callable = lambda self: print(self)
28+
29+
2030
if __name__ == "__main__":
2131
s1 = Sub1(based="based", sub="sub")
2232
print(s1)
2333

2434
# Not a dataclass, so that the fields are not overwritten.
2535
s2 = Sub2(based="based")
2636
print(s2)
37+
38+
print(HasMethods(3))
39+
print(HasMethods(2, None))
40+
HasMethods(2, None).e()
41+
print(type(print))

0 commit comments

Comments
Β (0)