[code.view]

[top] / python / PyMOTW / collections / collections_namedtuple_person.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2009 Doug Hellmann All rights reserved.
     #
     """
     """
     #end_pymotw_header
     
     import collections
     
     Person = collections.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 '\nFields by index:'
     for p in [ bob, jane ]:
         print '%s is a %d year old %s' % p
         

[top] / python / PyMOTW / collections / collections_namedtuple_person.py

contact | logmethods.com