Skip to content

Commit 6a4bb45

Browse files
data class added
1 parent 966f76c commit 6a4bb45

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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)

0 commit comments

Comments
 (0)