Created
          May 16, 2017 15:27 
        
      - 
      
- 
        Save Everfighting/49f5e92fce7ed35e61e4d2fbca5526f5 to your computer and use it in GitHub Desktop. 
    元组元素命名,使得下标更有含义
  
        
  
    
      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
    
  
  
    
  | from collections import namedtuple | |
| Student = namedtuple('Student',['name','age','sex','email']) | |
| # 位置传参 | |
| s = Student('Jim',16,'male','[email protected]') | |
| # 关键字传参 | |
| s2 = Student(name ='Jim', age = 16, sex = 'male', email = '[email protected]') | |
| # 类对象访问元组 | |
| s.name | |
| # isinstance(s,tuple) | |
| True | 
  
    
      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
    
  
  
    
  | NAME,AGE,SEX,EMAIL = range(4) | |
| student = ('jim',16,'male','[email protected]') | |
| # name | |
| print(student[NAME]) | |
| # age | |
| if student[AGE] >= 18: | |
| # sex | |
| if student[SEX] == 'male': | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment