439,957 Members | 1,969 Online Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

 P: n/a Python newbie advice needed! I'm tring to write what I would have expressed in Perl as my (\$a, \$b, \$c) = @array; This is very similar to the python statement a, b, c = array BUT, the Python will throw an exception if array isn't exactly 3 elements long, wheras the Perl will work for any length of @array, either throwing away excess elements or setting the variables to undef, ie like this if len(array) >= 1: a = array else: a = None if len(array) >= 2: b = array else: b = None if len(array) >= 3: c = array else: c = None This works if array has >= 3 elements a, b, c = array[:3] And this works however many elements array has a, b, c = (array + 3*[None])[:3] but it doesn't seem very Pythonic - is there a better way? -- Nick Craig-Wood nc*@axis.demon.co.uk Jul 18 '05 #1
4 Replies

 P: n/a In the specific case where you're trying to map a variable-length argument list to local variables in a function, you can do the more Pythonic: def myfunc (a = None, b = None, c = None) I'd say the answer is similar for other tasks. There's probably a Pythonic way to do the overall task as opposed to a statement-by-statement translation. I'm tring to write what I would have expressed in Perl as my (\$a, \$b, \$c) = @array; This is very similar to the python statement a, b, c = array BUT, the Python will throw an exception if array isn't exactly 3 elements long, wheras the Perl will work for any length of @array, either throwing away excess elements or setting the variables to undef, ie like this if len(array) >= 1: a = array else: a = None if len(array) >= 2: b = array else: b = None if len(array) >= 3: c = array else: c = None This works if array has >= 3 elements a, b, c = array[:3] And this works however many elements array has a, b, c = (array + 3*[None])[:3] but it doesn't seem very Pythonic - is there a better way? -- Nick Craig-Wood nc*@axis.demon.co.uk -- http://mail.python.org/mailman/listinfo/python-list Jul 18 '05 #2

 P: n/a "Nick Craig-Wood" wrote in message news:sl****************@irishsea.home.craig-wood.com... Python newbie advice needed! I'm tring to write what I would have expressed in Perl as my (\$a, \$b, \$c) = @array; This is very similar to the python statement a, b, c = array BUT, the Python will throw an exception if array isn't exactly 3 elements long, This is an intentional feature. Mismatches are often bugs. wheras the Perl will work for any length of @array, either throwing away excess elements or setting the variables to undef, ie like this Guido's philosophy is that the interpreter should resist guessing like this when code is at least half likely to be buggy. And this works however many elements array has a, b, c = (array + 3*[None])[:3] but it doesn't seem very Pythonic - is there a better way? Being explicitly generic is Pythonic to me. Or do something like a=b=c=None try: c=array try: b=array try: a=array except: pass except: pass except: pass but I prefer the one liner here. It is easier to extend to more names. Terry J. Reedy Jul 18 '05 #3

 P: n/a Wayne Folta wrote: In the specific case where you're trying to map a variable-length argument list to local variables in a function, you can do the more Pythonic: def myfunc (a = None, b = None, c = None) Interesting... I was thinking in particular of sys.argv with this example, but the above gave me the idea below which would work quite well (imagine sys.argv in place of L below) def f(a=None,b=None,c=None,*extra): print a,b,c .... L=[]; apply(f,L) None None None L=; apply(f,L) 1 None None L=[1,2]; apply(f,L) 1 2 None L=[1,2,3]; apply(f,L) 1 2 3 L=[1,2,3,4]; apply(f,L) 1 2 3 L=[1,2,3,4,5]; apply(f,L) 1 2 3 -- Nick Craig-Wood nc*@axis.demon.co.uk Jul 18 '05 #4

 P: n/a "Nick Craig-Wood" wrote in message news:sl***************@irishsea.home.craig-wood.com... def myfunc (a = None, b = None, c = None) def f(a=None,b=None,c=None,*extra): print a,b,c ... L=[]; apply(f,L) You can now write 'apply(f,L)' as f(*L) and be future-proof in case apply disappears tjr Jul 18 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more Python Questions on Bytes 