How to use namedtuple to create a tuple with named fields in Python

2 Answers

0 votes
import collections

Worker = collections.namedtuple("Worker", ["id", "name", "salary"])

w = Worker(123, "tom", 13874)

print(w)

 
'''
run:
 
Worker(id=123, name='tom', salary=13874)
 
'''

 



answered Nov 4, 2018 by avibootz
0 votes
import collections

Worker = collections.namedtuple("Worker", ["id", "name", "salary"])

w = Worker(123, "tom", 13874)

print(w.id)
print(w.name)
print(w.salary)

 
'''
run:
 
123
tom
13874
 
'''

 



answered Nov 4, 2018 by avibootz

Related questions

...