473,394 Members | 1,916 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,394 software developers and data experts.

Python style advice

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?

--
Nick Craig-Wood
nc*@axis.demon.co.uk
Jul 18 '05 #1
4 1203
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[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?

--
Nick Craig-Wood
nc*@axis.demon.co.uk
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #2

"Nick Craig-Wood" <nc*@axis.demon.co.uk> 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[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


Jul 18 '05 #3
Wayne Folta <wf****@netmail.to> 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=[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

--
Nick Craig-Wood
nc*@axis.demon.co.uk
Jul 18 '05 #4

"Nick Craig-Wood" <nc*@axis.demon.co.uk> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Todd7 | last post by:
I am looking at learning Python, but I would like to know what its strengths and weaknesses are before I invest much time in learning it. My first thought is what is it good for programming,...
20
by: walterbyrd | last post by:
Reading "Think Like a Computer Scientist" I am not sure I understand the way it describes the way objects work with Python. 1) Can attributes can added just anywhere? I create an object called...
8
by: Derek Martin | last post by:
I'd like to know if it's possible to code something in Python which would be equivalent to the following C: ---- debug.c ---- #include <stdio.h> bool DEBUG;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.