-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection Module - namedtuple.py
More file actions
100 lines (36 loc) · 899 Bytes
/
Collection Module - namedtuple.py
File metadata and controls
100 lines (36 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# named tuple can assign names as well as numerical index
# in the normal tuple you can not assign name
# In[2]:
t = (1,2,3)
# In[3]:
t[0]
# In[4]:
from collections import namedtuple
# In[8]:
# you have to assign type name and field name like class
Dog = namedtuple('Dog','age breed name')
# now age , breed and name is a constructor of a class Dog
# its a fast method to create class
# In[7]:
sam = Dog(age=2 , breed = 'lab' , name = 'Sammy')
# In[12]:
sam
# In[13]:
sam.age
# In[20]:
# u can get by index aswell like in tupple
sam[0] #referes to age
# In[32]:
# you have to name the variable same as class name otherwise it will not work
Cat = namedtuple('Cat','fur claws name')
# In[35]:
c = Cat(fur = 'a' , claws = 'b' , name = 'c')
# In[37]:
c[0]
# In[38]:
c.fur
# In[ ]:
# In[ ]: