Created
          May 17, 2017 01:17 
        
      - 
      
- 
        Save Everfighting/80335befc3e7e301c83df5410f30b31a to your computer and use it in GitHub Desktop. 
    namedtuple example
From https://docs.python.org/3/library/collections.html#collections.namedtuple
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | >>> # Basic example | |
| >>> Point = namedtuple('Point', ['x', 'y']) | |
| >>> p = Point(11, y=22) # instantiate with positional or keyword arguments | |
| >>> p[0] + p[1] # indexable like the plain tuple (11, 22) | |
| 33 | |
| >>> x, y = p # unpack like a regular tuple | |
| >>> x, y | |
| (11, 22) | |
| >>> p.x + p.y # fields also accessible by name | |
| 33 | |
| >>> p # readable __repr__ with a name=value style | |
| Point(x=11, y=22) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment