Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value in Python unpacking

Tags:

Is there a way to have a default value if the number of values to unpack is too little compared to the variable list?

For example:

a, b, c = read_json(request)

This works if read_json returns an array of three or more variable. If it only returns two, I get an exception while assigning c. So, is there a way to set c to a default value if it can't be unpacked properly? Something like:

a, b, (c=2) = read_json(request)

Which is similar to what you do when defining a function with default arguments.

Thank you!

like image 888
Laurent Avatar asked Aug 12 '15 06:08

Laurent


People also ask

What is unpacking of data in Python?

Introduction. Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list ) of variables in a single assignment statement. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, * .

What is the unpacking operator in Python?

Both * and ** are the operators that perform packing and unpacking in Python. The * operator (quite often associated with args) can be used with any iterable (such as a tuple, list and strings), whereas the ** operator, (quite often associated with kwargs) can only be used on dictionaries.

What is default value of variable in Python?

Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.


2 Answers

You could try * unpacking with some post-processing:

a, b, *c = read_json(request)
c = c[0] if c else 2

This will assign a and b as normal. If c is assigned something, it will be a list with one element. If only two values were unpacked, it will be an empty list. The second statement assigns to c its first element if there is one, or the default value of 2 otherwise.

>>> a, b, *c = 1, 2, 3
>>> c = c[0] if c else 2
>>> a
1
>>> b
2
>>> c
3
>>> a, b, *c = 1, 2
>>> c = c[0] if c else 2
>>> a
1
>>> b
2
>>> c
2
like image 105
TigerhawkT3 Avatar answered Sep 21 '22 15:09

TigerhawkT3


You can use chain function from itertools, which is part of the Python standard library. It serve as default filler in case if there are no values in the first list. 'defaults' list variable in my example can have number of different values for each variable that you unpack (in an example I have default value for all three values as 0).

from itertools import chain

defaults = [0] * 3
data = []

a, b, c, *_ = chain(data, defaults)
print(a, b, c)

data.append(1)
a, b, c, *_ = chain(data, defaults)
print(a, b, c)

data.append(2)
a, b, c, *_ = chain(data, defaults)
print(a, b, c)

data.append(3)
a, b, c, *_ = chain(data, defaults)
print(a, b, c)

data.append(4)
a, b, c, *_ = chain(data, defaults)
print(a, b, c)

Outputs:

0 0 0
1 0 0
1 2 0
1 2 3
1 2 3
like image 44
Vlad Bezden Avatar answered Sep 22 '22 15:09

Vlad Bezden