Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Unpack less values from function's return values

Reply
Thread Tools

Re: Unpack less values from function's return values

 
 
Chris Rebert
Guest
Posts: n/a
 
      05-28-2009
On Thu, May 28, 2009 at 3:19 AM, <(E-Mail Removed)> wrote:
> Hi,
>
> I'm using Python 2.5.2. I'm getting this error whenever I try to unpack less
> values from a function.
>
> ValueError: too many values to unpack
>
>
> I want to know if there is a way I can unpack less values returning from a
> function?


Unpack them into throwaway variables:

def foo(): return 1,2,3,4

a, b, _, _ = foo()

In very new Python, you can also do:

a, b, *_ = foo()

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Bobby
Guest
Posts: n/a
 
      05-28-2009
On May 28, 5:40*am, Chris Rebert <(E-Mail Removed)> wrote:
> On Thu, May 28, 2009 at 3:19 AM, *<(E-Mail Removed)> wrote:
> > Hi,

>
> > I'm using Python 2.5.2. I'm getting this error whenever I try to unpack less
> > values from a function.

>
> > ValueError: too many values to unpack

>
> > I want to know if there is a way I can unpack less values returning from a
> > function?

>
> Unpack them into throwaway variables:
>
> def foo(): return 1,2,3,4
>
> a, b, _, _ = foo()
>
> In very new Python, you can also do:
>
> a, b, *_ = foo()
>
> Cheers,
> Chris
> --http://blog.rebertia.com


You could also do something like
a,b = foo()[:2]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
More Efficiency,More Benefit,Less Risk,Less Work! jiajia wu ASP .Net 0 10-01-2009 01:50 PM
More Efficiency,More Benefit,Less Risk,Less Work! lllll Ruby 0 06-08-2009 02:10 PM
More Efficiency,More Benefit,Less Risk,Less Work! 6668 Ruby 0 05-14-2009 12:33 AM
struct.unpack less than 1 byte cprogrammer Python 4 10-10-2007 02:00 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments