Python的namedtuple使用

2021-06-22 08:05

阅读:242

标签:field   ati   function   gen   func   arguments   tuple   end   import   

from collections import namedtuple

# Each kind of nametuple is represented by its own class,
# created by using the nametuple() factory function
# The arguments are the name of the new class and a string 
# containing the names of the elements

Person = namedtuple(‘Person‘, ‘name age gender‘)

print ‘Type of Person:‘, type(Person)

bob = Person(name=‘Bob‘, age=30, gender=‘male‘)
print ‘\nRepresentation:‘, bob

jane = Person(name=‘Jane‘, age=29, gender=‘female‘)
print ‘\nField by name:‘, jane.name

print ‘\nField by index:‘
for p in [bob, jane]:
    print ‘%s is a %d year old %s‘ % p

 

Python的namedtuple使用

标签:field   ati   function   gen   func   arguments   tuple   end   import   

原文地址:https://www.cnblogs.com/Shinered/p/9678797.html


评论


亲,登录后才可以留言!