100 lines
1.8 KiB
ReStructuredText
100 lines
1.8 KiB
ReStructuredText
>>> spam = 1, 2, 3
|
|
>>> eggs = 4, 5, 6
|
|
|
|
>>> data = dict()
|
|
>>> data[spam] = 'spam'
|
|
>>> data[eggs] = 'eggs'
|
|
|
|
>>> import pprint # Using pprint for consistent and sorted output
|
|
|
|
>>> pprint.pprint(data)
|
|
{(1, 2, 3): 'spam', (4, 5, 6): 'eggs'}
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
>>> spam = 1, 'abc', (2, 3, (4, 5)), 'def'
|
|
>>> eggs = 4, (spam, 5), 6
|
|
|
|
>>> data = dict()
|
|
>>> data[spam] = 'spam'
|
|
>>> data[eggs] = 'eggs'
|
|
>>> import pprint # Using pprint for consistent and sorted output
|
|
|
|
>>> pprint.pprint(data)
|
|
{(1, 'abc', (2, 3, (4, 5)), 'def'): 'spam',
|
|
(4, ((1, 'abc', (2, 3, (4, 5)), 'def'), 5), 6): 'eggs'}
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
# Assign using tuples on both sides
|
|
|
|
>>> a, b, c = 1, 2, 3
|
|
>>> a
|
|
1
|
|
|
|
# Assign a tuple to a single variable
|
|
|
|
>>> spam = a, (b, c)
|
|
>>> spam
|
|
(1, (2, 3))
|
|
|
|
# Unpack a tuple to two variables
|
|
|
|
>>> a, b = spam
|
|
>>> a
|
|
1
|
|
>>> b
|
|
(2, 3)
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
# Unpack with variable length objects which assigns a list instead
|
|
of a tuple
|
|
|
|
>>> spam, *eggs = 1, 2, 3, 4
|
|
>>> spam
|
|
1
|
|
>>> eggs
|
|
[2, 3, 4]
|
|
|
|
# Which can be unpacked as well of course
|
|
|
|
>>> a, b, c = eggs
|
|
>>> c
|
|
4
|
|
|
|
# This works for ranges as well
|
|
|
|
>>> spam, *eggs = range(10)
|
|
>>> spam
|
|
0
|
|
>>> eggs
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
# And it works both ways
|
|
|
|
>>> a, b, *c = a, *eggs
|
|
>>> a, b
|
|
(2, 1)
|
|
>>> c
|
|
[2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
>>> def eggs(*args):
|
|
... print('args:', args)
|
|
|
|
>>> eggs(1, 2, 3)
|
|
args: (1, 2, 3)
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
>>> def spam_eggs():
|
|
... return 'spam', 'eggs'
|
|
|
|
>>> spam, eggs = spam_eggs()
|
|
>>> spam
|
|
'spam'
|
|
>>> eggs
|
|
'eggs'
|