View Full Version : Which is more Pythonic?
slumcat05
July 5th, 2007, 03:50 PM
OK, I just started learning Python, and I love it. I'm a longtime Matlab user, and am novice/intermediate with C (i.e. I've had a 100 level course oriented towards aerospace engineers). I'm trying to practice the whole concept of Pythonic... (ness?). I know that in many cases, the best way to do something is generally the only, way, and hence the most Pythonic. However there is one case that I am confused about, and I'm hoping any Python pros can offer some advice. Take for example the following two codes snippets:
Method 1:
a = 1
b = 2
print "The value of a is", a, "and the value of b is", b
Method 2:
a = 1
b = 2
print "The value of a is %i and the value of b is %i" % (a, b)
Both of these produce the exact same message on the screen, 'The value of a is 1 and the value of b is 2'. My internal debate is this: I'm familiar with the %i, %d, %s, etc. placeholders from C and Matlab, and this method is a little bit quicker to type (fewer double-qoutes and commas). I can't decide, however, which is more readable. To me, both methods are pretty straightforward, so Method 2 seems more desirable (like I said, a little quicker to type), but it seems like a person unfamiliar with the placeholders from C, Matlab, etc. may have trouble seeing what is going on in Method 2.
Can any pros please throw in their $0.02 (by the way, what ever happened to the "cent" symbol? Does nobody use that anymore?). Is there a preffered method? Is one faster or less memory intensive than the other? Are there certain times when one should be used, and other times when the other should be used? Or is this just a unique case where there is more than one way to do it, and it's completely up to the programmer to decide which is best or most convenient? Thanks for any input/opinions.
pointone
July 5th, 2007, 04:06 PM
I'm not really a Python 'pro', so I hope you don't mind me chipping in... :P
I always try to use string formatting because it's much more powerful. You can, for instance, use a dictionary to replace values rather than specify each value individually. For example:
z = {"a": 1, "b": 2}
print "The value for a is %(a)i and the value of b is %(b)i" % z
While this might not seem like a big deal now, it helps a lot in larger projects where your values might be constantly changing, or where it's easier to package everything together in a dictionary (or where output is a dictionary by default). I also find it easier to read because "print a" doesn't tell you anything about the value of a, whereas "print '%i' % a" shows that the value will be an integer (of course, I wouldn't use string formatting in a case this simple; it's just an example).
Overall, though, it's more a matter of personal preference. Whichever is easier for you is the best solution, in my opinion.
pmasiar
July 5th, 2007, 07:28 PM
Let's define what we are talking about
tuples:
print 'text', value
String interpolation:
print "text %i more ]%s[ text" % (ivalue, svalue) or
print "text %(NAME)i more text" % dict(NAME=ivalue)
There is one more slight difference: Print using tuples adds a space between text and value. Interpolation gives you more control over formatting (including no extra spaces).
So it depends - and most time I use excellent templating system Kid to output my text (webpages).
There is no strict rule about usage, but *I* most often use tuples for debug messages, and interpolation for regular output (and if using dicts, I use keys ALLCAP to show where values go).
Tuples are probably simpler (Python does not need to create new strings with interpolated values), but difference is OMHO very minor and not worth bother.
Note360
July 5th, 2007, 09:41 PM
pythonic is kinda up to interpretation for somethings. Obviously some ways are more right than others, but in the end its really prefrence. I think python needs something like ruby's "#{variable}". I would suggest %[var]formatting_stuff, this way we get a compact way to do it that looks pretty tame for string formatting.
pmasiar
July 5th, 2007, 10:23 PM
What ruby's "#{variable}" does what cannot be done as easy with string interpolation?
xtacocorex
July 5th, 2007, 10:43 PM
(i.e. I've had a 100 level course oriented towards aerospace engineers).
Everyone knows that all Aerospace Engineers need to learn FORTRAN, I myself am one of the proud few. In fact, it is in the program at Iowa State from day one. Beside knowing a language 80% of the world forgot about, you'll get the added satisfaction of using a language that ran on punch cards, not something most other languages can offer you.
You can also do the cool OOP that everyone is talking about these days with FORTRAN and would only have to worry about what your original question deals with if you use FORMAT to set up output formatting.
kripkenstein
July 6th, 2007, 05:50 AM
Well, in Python3000 (Python 3.0, coming out in a year or so), this will not be relevant anymore as the 'print' statement is changing. It will be a function, and other things.
So, the real pythonic way to write that is yet to come ;)
But meanwhile both of your examples look fine to me.
cwaldbieser
July 6th, 2007, 08:22 AM
pythonic is kinda up to interpretation for somethings. Obviously some ways are more right than others, but in the end its really prefrence. I think python needs something like ruby's "#{variable}". I would suggest %[var]formatting_stuff, this way we get a compact way to do it that looks pretty tame for string formatting.
In python 2.4, the string.Template class allows for "shell-style" substitution.
import string
t = string.Template("Hello, ${thing}!")
print t.substitute(thing='World')
LaRoza
July 6th, 2007, 09:03 AM
ets:
Can any pros please throw in their $0.02 (by the way, what ever happened to the "cent" symbol? Does nobody use that anymore?) ... Thanks for any input/opinions.
The entity to use is "¢"; however, it is disabled in this forum. (It converts the initial "&" into ints entity, which is "&"
slumcat05
July 6th, 2007, 11:03 AM
Thanks for all the feedback. I guess the general consensus is that there's no general consensus. I did, however, learn a few things, such as the string interpolation method with dictionaries, which I hadn't seen yet, so thanks to all.
Everyone knows that all Aerospace Engineers need to learn FORTRAN
Yeah, I've seen some of that. In one of my internships at NASA my mentor wanted me to do all my work in FORTRAN. Fortunately for me it was only a 6 week internship (not much time to learn a new language and still get work done), so he had mercy and allowed me to use Matlab. Since then I've used a lot of NASA tools that were written in FORTRAN (I just compiled and moved on), but in my experience so far with Boeing it seems like the trend, at least in the commercial sector, is moving towards Matlab; so I'm glad that my undergrad classes focused on that. In fact I'm rather surprised that Iowa State is teaching it.
xtacocorex
July 6th, 2007, 12:32 PM
Definitely way off topic here. Iowa State's program is very numerical method oriented (aside from Aerodynamics) and programming is one of the big weed out factors early on. They are starting to use more Matlab though with the newer classes being brought in. The only class I used it in was Flight Controls.
vBulletin® v3.8.0 Release Candidate 2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.