Python style advice

  • Thread starter Nick Craig-Wood
  • Start date
N

Nick Craig-Wood

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[0]
else:
a = None
if len(array) >= 2:
b = array[1]
else:
b = None
if len(array) >= 3:
c = array[2]
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?
 
W

Wayne Folta

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.
 
T

Terry Reedy

Nick Craig-Wood said:
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[2]
try: b=array[1]
try: a=array[1]
except: pass
except: pass
except: pass

but I prefer the one liner here. It is easier to extend to more names.

Terry J. Reedy
 
N

Nick Craig-Wood

Wayne Folta said:
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=[1]; 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top