473,484 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

unpacking with default values

McA
Hi all,

probably a dumb question, but I didn't find something elegant for my
problem so far.
In perl you can unpack the element of a list to variables similar as
in python
(a, b, c = [0, 1, 2]), but the number of variables need not to fit the
number
of list elements.
That means, if you have less list elements variables are filled with
'undef' (None in python), if you have more list elements as necessary
the rest is ignored.

How can I achieve this behaviour with python in an elegant and fast
way?

Best regards
Andreas Mock
Jul 17 '08 #1
4 4584
McA wrote:
Hi all,

probably a dumb question, but I didn't find something elegant for my
problem so far.
In perl you can unpack the element of a list to variables similar as
in python
(a, b, c = [0, 1, 2]), but the number of variables need not to fit the
number
of list elements.
That means, if you have less list elements variables are filled with
'undef' (None in python), if you have more list elements as necessary
the rest is ignored.

How can I achieve this behaviour with python in an elegant and fast
way?

Best regards
Andreas Mock
--
http://mail.python.org/mailman/listinfo/python-list
Python 3.0 has something a bit like this. Excess values can be bound
(as a list) to the last variable:

a,b,*c = [1,2,3,4,5]

will result in c containing [3,4,5].

In Python 2.x, you can't do that directly, but you should be able to
create a function that lengthens or shortens an input tuple of arguments
to the correct length so you can do:

a,c,b = fix(1,2)
d,e,f = fix(1,2,3,4)

However, the function won't know the length of the left hand side
sequence, so it will have to be passed in as an extra parameter or hard
coded.
Gary Herron

Jul 17 '08 #2
McA
On 17 Jul., 18:33, Gary Herron <gher...@islandtraining.comwrote:
>
In Python 2.x, you can't do that directly, but you should be able to
create a function that lengthens or shortens an input tuple of arguments
to the correct length so you can do:

a,c,b = fix(1,2)
d,e,f = fix(1,2,3,4)

However, the function won't know the length of the left hand side
sequence, so it will have to be passed in as an extra parameter or hard
coded.
Hi Gary,

thank you for the answer.
Do you know the "protocol" used by python while unpacking?
Is it a direct assingnment? Or iterating?

Best regards
Andreas Mock
Jul 17 '08 #3
McA wrote:
On 17 Jul., 18:33, Gary Herron <gher...@islandtraining.comwrote:
>In Python 2.x, you can't do that directly, but you should be able to
create a function that lengthens or shortens an input tuple of arguments
to the correct length so you can do:

a,c,b = fix(1,2)
d,e,f = fix(1,2,3,4)

However, the function won't know the length of the left hand side
sequence, so it will have to be passed in as an extra parameter or hard
coded.

Hi Gary,

thank you for the answer.
Do you know the "protocol" used by python while unpacking?
Is it a direct assingnment? Or iterating?
Both I think, but what do you mean by *direct* assignment?
Best regards
Andreas Mock
--
http://mail.python.org/mailman/listinfo/python-list
It RHS of such an assignment can be any iterable (I think). Lets test:
(The nice thing about an interactive Python session, it that it's really
easy to test.)

>>L = [1,2,3]
a,b,c=L
a,b=L
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>a,b,c,d=L
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 3 values to unpack
>>G = (f for f in [1,2,3]) # A generator expression
a,b,c = G
G = (f for f in [1,2,3]) # A generator expression
a,b = G
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>G = (f for f in [1,2,3]) # A generator expression
a,b,c,d = G
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 3 values to unpack
I'd call that direct assignment with values supplied by any iterable.

Gary Herron

Jul 17 '08 #4


McA wrote:
Do you know the "protocol" used by python while unpacking?
Is it a direct assingnment? Or iterating?
In CPython, at least, both, just as with normal unpack and multiple
assignment. The iterable is unpacked into pieces by iterating (with
knowledge of the number of targets and which is the catchall). The
targets are then directly bound.
>>from dis import dis
# first standard assignment
>>dis(compile("a,b,c = range(3)", '','single'))
1 0 LOAD_NAME 0 (range)
3 LOAD_CONST 0 (3)
6 CALL_FUNCTION 1
9 UNPACK_SEQUENCE 3
12 STORE_NAME 1 (a)
15 STORE_NAME 2 (b)
18 STORE_NAME 3 (c)
21 LOAD_CONST 1 (None)
24 RETURN_VALUE
# now starred assignment
>>dis(compile("a,b,*c = range(3)", '','single'))
1 0 LOAD_NAME 0 (range)
3 LOAD_CONST 0 (3)
6 CALL_FUNCTION 1
9 UNPACK_EX 2
12 STORE_NAME 1 (a)
15 STORE_NAME 2 (b)
18 STORE_NAME 3 (c)
21 LOAD_CONST 1 (None)
24 RETURN_VALUE

The only difference is UNPACK_EX (tended) instead of UNPACK_SEQUENCE.
Tne UNPACK_EX code is not yet in the dis module documentation.
But a little reverse engineering reveals the parameter meaning:
a,*b,c and *a,b,c give parameters 257 and 512 instead of 2.
Separating 2,257,512 into bytes gives 0,2; 1,1; 2,0.
a,*b and *a,b give 1, 256 or 0,1; 1,0. The two bytes
are the number of targets after and before the starred target.

Terry Jan Reedy
Jul 17 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1748
by: george young | last post by:
I came across an cool python feature that I've not seen discussed. This may be *implied* by the language reference manual http://docs.python.org/ref/assignment.html], but it was never obvious to...
8
3609
by: Paul McGuire | last post by:
I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList = for data in configList: name,a,b,c = data ... do something...
3
4537
by: Geoffrey | last post by:
I am working on a file conversion project that reads data from a one file format, reformats in and writes in out to another. The data is records of informations - names address, account...
5
1971
by: Chris | last post by:
Hi I'm attempting to write a client for an existing p2p network. The protocol defines that ints are packed into 4 bytes for transfer. // Creating the byte vector using the following is fine:...
9
1404
by: tkpmep | last post by:
I have a list y >>>y from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I tried >>>a,b = y Traceback (most recent call last): File "<interactive...
11
1603
by: harold | last post by:
Dear all, Maybe I stared on the monitor for too long, because I cannot find the bug ... My script "transition_filter.py" starts with the following lines: import sys for line in sys.stdin :...
16
1318
by: John Salerno | last post by:
I'm a little confused, but I'm sure this is something trivial. I'm confused about why this works: ('more', 'less'), ('something', 'nothing'), ('good', 'bad')) (('hello', 'goodbye'), ('more',...
5
1503
by: ram | last post by:
Stupid question #983098403: I can't seem to pass an unpacked sequence and keyword arguments to a function at the same time. What am I doing wrong? def f(*args, **kw): for a in args: print...
3
1915
by: Chris Garland | last post by:
What's wrong here? I can unpack an unsigned char (144,) I can unpack a short (6,) But an unsigned char & a short give me this
0
7079
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7137
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6809
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7194
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4838
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3044
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1355
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.