python c like structs

Solutions on MaxInterview for python c like structs by the best coders in the world

showing results for - "python c like structs"
Vincent
14 May 2019
1from typing import NamedTuple
2
3
4class User(NamedTuple):
5    name: str
6
7
8class MyStruct(NamedTuple):
9    foo: str
10    bar: int
11    baz: list
12    qux: User
13
14
15my_item = MyStruct('foo', 0, ['baz'], User('peter'))
16
17print(my_item) # MyStruct(foo='foo', bar=0, baz=['baz'], qux=User(name='peter'))
Axel
08 Mar 2018
1from dataclasses import dataclass
2
3
4@dataclass
5class Point:
6    x: float
7    y: float
8    z: float = 0.0
9
10
11p = Point(1.5, 2.5)
12
13print(p)  # Point(x=1.5, y=2.5, z=0.0)