Advertisement
nher1625

more star unpacking

Mar 25th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. __author__ = 'Work'
  2.  
  3. '''
  4. Star unpacking combined with string processing operations, such as splitting.
  5. '''
  6.  
  7. line = "nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false"
  8. uname, *fields, homedir, sh = line.split(':')
  9. print('username: ', uname, '\n',
  10.       'homedir: ', homedir, '\n',
  11.       'sh: ', sh, '\n',
  12.       'fields: ', fields)
  13.  
  14. '''
  15. Unpacking variables and throwing them away. Use a common throwaway variable name, such as _ or ign (ignored).
  16. Ex:
  17. '''
  18.  
  19. record = [ 'ACME', 50, 123.45, (12, 18, 2012) ]
  20. name, *throwaway_0, (*throwaway_1, year) = record
  21. print(
  22.     'name: ', name, '\n',
  23.     'first throwaway variable: ', throwaway_0, '\n',
  24.     'second throwaway variable: ', throwaway_1, '\n',
  25.     'year:', year, '\n'
  26. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement