473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Rats! vararg assignments don't work

I'm a relative newbie to Python, so please bear with me. After seeing
how varargs work in parameter lists, like this:
def func(x, *arglist):
and this:
x = func(1, *moreargs)
I thought that I'd try this:
first, *rest = arglist
Needless to say, it didn't work. That leaves me with two questions.

First, is there a good way to do this? For now, I'm using this:
first, rest = arglist[0], arglist[1:]
but it leaves a bad taste in my mouth.

Second, is there any good reason why it shouldn't work? It seems like
such an obvious idiom that I can't believe that I'm the first to come up
with the idea. I don't really have the time right now to go source
diving, so I can't tell if it would be wildly inefficient to implement.

Thanks!
May 30 '07 #1
11 1358
Your attemtp:

Expand|Select|Wrap|Line Numbers
  1. first, rest = arglist[0], arglist[1:]
  2.  
Is the most obvious and probably the most accepted way to do what you
are looking for. As for adding the fucntionality you first suggested,
it isn't likely to be implemented. The first step would be to write a
PEP though. Remember, in Python "there is only one way to do it". So,
unless you can come up with a valid use case where that syntax allows
you to do something that wasn't possible before, I wouldn't count on
getting much support.
May 30 '07 #2
On May 29, 11:33 pm, Matimus <mccre...@gmail.comwrote:
Your attemtp:

Expand|Select|Wrap|Line Numbers
  1. first, rest = arglist[0], arglist[1:]
  2.  

Is the most obvious and probably the most accepted way to do what you
are looking for. As for adding the fucntionality you first suggested,
it isn't likely to be implemented. The first step would be to write a
PEP though.
The time machine did it again: http://www.python.org/dev/peps/pep-3132/.

George

May 30 '07 #3
samwyse wrote:
I'm a relative newbie to Python, so please bear with me. After seeing
how varargs work in parameter lists, like this:
def func(x, *arglist):
and this:
x = func(1, *moreargs)
I thought that I'd try this:
first, *rest = arglist
Needless to say, it didn't work. That leaves me with two questions.

First, is there a good way to do this? For now, I'm using this:
first, rest = arglist[0], arglist[1:]
but it leaves a bad taste in my mouth.
Well, your moreargs parameter is a tuple, and there are innumerable ways
to process a tuple. (And even more if you convert it to a list.)

If you are just interested in extracting only the first arg, then your
code is quite Pythonic. However, if you are going to do that in a loop
to successively process each arg, the you have several better options:

For instance:
for arg in moreargs: # Loop through each arg
<do something with arg>

or

for i in range(len(moreargs)):
<do something with morergs[i] # Extract ith arg

or

argslist = list(moreargs)
while argslist:
firstarg = argslist.pop(0) # Extract first arg
<do something with firstarg>

Gary Herron
Second, is there any good reason why it shouldn't work? It seems like
such an obvious idiom that I can't believe that I'm the first to come up
with the idea. I don't really have the time right now to go source
diving, so I can't tell if it would be wildly inefficient to implement.

Thanks!
May 30 '07 #4
Gary Herron wrote:
samwyse wrote:
>>I'm a relative newbie to Python, so please bear with me. After seeing
how varargs work in parameter lists, like this:
def func(x, *arglist):
and this:
x = func(1, *moreargs)
I thought that I'd try this:
first, *rest = arglist
Needless to say, it didn't work. That leaves me with two questions.

First, is there a good way to do this? For now, I'm using this:
first, rest = arglist[0], arglist[1:]
but it leaves a bad taste in my mouth.

Well, your moreargs parameter is a tuple, and there are innumerable ways
to process a tuple. (And even more if you convert it to a list.)
My use-case is (roughtly) this:
first, *rest = f.readline().split()
return dispatch_table{first}(*rest)
May 30 '07 #5
George Sakkis wrote:
On May 29, 11:33 pm, Matimus <mccre...@gmail.comwrote:

>>Your attemtp:

Expand|Select|Wrap|Line Numbers
  1. first, rest = arglist[0], arglist[1:]

Is the most obvious and probably the most accepted way to do what you
are looking for. As for adding the fucntionality you first suggested,
it isn't likely to be implemented. The first step would be to write a
PEP though.


The time machine did it again: http://www.python.org/dev/peps/pep-3132/.
Thanks! Now I just need to wait for Py3K and all of my problems will be
solved. ;-)

Actually, I'm surprised that the PEP does as much as it does. If tuples
are implemented as S-expressions, then something like this:
car, *cdr = tuple
while leaving cdr a tuple would be trivial to implement. Of course, I'm
an old-school LISPer, so what I consider surprising behavior doesn't
always surprise anyone else, and vice versa.
May 30 '07 #6
George Sakkis wrote:
The time machine did it again: http://www.python.org/dev/peps/pep-3132/.

Uhm, John Swartzwelder, right?

:D
/W
May 30 '07 #7
Matimus a écrit :
(snip)
Remember, in Python "there is only one way to do it".
Actually, it's :
"There should be one-- and preferably only one --obvious way to do it.".

.... Which is quite different. Please notice the "should", "preferably"
and "obvious".
May 30 '07 #8
samwyse a écrit :
George Sakkis wrote:
>On May 29, 11:33 pm, Matimus <mccre...@gmail.comwrote:

>>Your attemtp:

Expand|Select|Wrap|Line Numbers
  1. first, rest = arglist[0], arglist[1:]

Is the most obvious and probably the most accepted way to do what you
are looking for. As for adding the fucntionality you first suggested,
it isn't likely to be implemented. The first step would be to write a
PEP though.

The time machine did it again: http://www.python.org/dev/peps/pep-3132/.

Thanks! Now I just need to wait for Py3K and all of my problems will be
solved. ;-)

Actually, I'm surprised that the PEP does as much as it does. If tuples
are implemented as S-expressions, then something like this:
car, *cdr = tuple
while leaving cdr a tuple would be trivial to implement. Of course, I'm
an old-school LISPer, so what I consider surprising behavior doesn't
always surprise anyone else, and vice versa.
Remember all these are copies of the original sequence, the lisp
equivalent to car/cdr is feasible with an iterator :

it = iter(seq)
car, cdr = it.next(), it

May 30 '07 #9
samwyse <de******@email.comwrote:
>samwyse wrote:
>>>I thought that I'd try this:
first, *rest = arglist
Needless to say, it didn't work.
[ ... ]
My use-case is (roughtly) this:
first, *rest = f.readline().split()
return dispatch_table{first}(*rest)
first, rest = f.readline().split(None, 1)
return dispatch_table{first}(*rest.split())

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
May 30 '07 #10
samwyse <de******@email.comwrote:
...
Actually, I'm surprised that the PEP does as much as it does. If tuples
are implemented as S-expressions, then something like this:
Tuples are implemented as compact arrays of pointer-to-PyObject (so are
lists, BTW). So, for example, a 10-items tuple takes 40 bytes (plus a
small overhead for the header) on a 32-bit build, not 80 as it would if
implemented as a linked list of (pointer-to-object, pointer-to-next)
pairs; addressing sometuple[N] is O(1), NOT O(N); etc, etc.
Alex
May 30 '07 #11
On May 30, 7:29 am, Sion Arrowsmith <s...@chiark.greenend.org.uk>
wrote:
samwyse <dejan...@email.comwrote:
>samwysewrote:
I thought that I'd try this:
first, *rest = arglist
Needless to say, it didn't work.
[ ... ]
My use-case is (roughtly) this:
first, *rest = f.readline().split()
return dispatch_table{first}(*rest)

first, rest = f.readline().split(None, 1)
return dispatch_table{first}(*rest.split())
Hey, I like that! Thanks!

Jul 12 '07 #12

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

Similar topics

6
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise...
13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
0
by: levaquin for rats | last post by:
levaquin for rats levaquin antibiotic zyvox
4
by: anishap | last post by:
Hello, I have a table with the below data: ID Cat Rat Rabbits 001 A B D 002 A C E 003 B A A 004 A E B
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.