File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed
Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import NamedTuple
2+
3+ class MyWorld (NamedTuple ):
4+ name : str
5+ type : int = 0
6+
7+ my_world = MyWorld (name = 'Potato' , type = 10 )
8+ print (my_world .name , my_world .type )
9+
10+ my_world = MyWorld (name = 'Oh no' )
11+ print (my_world .name , my_world .type )
12+
13+ print (my_world ._fields )
14+
15+ new = my_world ._replace (name = 'Oh yes' )
16+ print (new )
17+
18+
19+ data = {'name' : "Qt" , 'type' : 3.14 }
20+ word = MyWorld (** data )
21+ print (word )
Original file line number Diff line number Diff line change 1+ from typing import List
2+ from dataclasses import dataclass
3+
4+ @dataclass
5+ class MyWorld :
6+ name : str
7+ type : int = 0
8+
9+ my_world = MyWorld (name = 'Potato' , type = 10 )
10+ print (my_world .name , my_world .type )
11+
12+ my_world = MyWorld (name = 'Oh no' )
13+ print (my_world .name , my_world .type )
14+
15+ data = {'name' : "Qt" , 'type' : 3.14 }
16+ word = MyWorld (** data )
17+ print (word )
18+
19+
20+ @dataclass
21+ class Food :
22+ name : str
23+
24+ def hows_it (self ):
25+ print ('{} is the best in the world' .format (self .name ) )
26+
27+ wada_pav = Food (name = 'Mumbai wadapav' )
28+ wada_pav .hows_it ()
29+
30+ @dataclass
31+ class Menu :
32+ dishes : List [Food ]
33+
34+
35+ menu = Menu (dishes = [wada_pav , wada_pav ])
36+ print (menu )
You can’t perform that action at this time.
0 commit comments