From Java to Python
January 13th, 2010
I’m not completely sure why, but I’m a bit embarrassed to admit to Planet Fedora how little my Python experience is; the majority of my experience is in Java. I was able to read and bug fix the Python code in Spacewalk, but I hadn’t really dug deep into my own project. Now that I’m not teaching any longer and have some free time (one of my main reasons for quitting), I can finally sit down and dork around with the language. After spending some time working on some basic games and a simple IRC bot, I figured I’d step back and think about what the transition from Java to Python has felt like.
Don’t Fear The Whitespace
I constantly hear people mention the indentation in Python as the first thing when talking about moving to the language. Not only is it not as jarring of an experience as people make it out to be, it’s downright awesome. I’ve always been compulsive about my code format anyway, so the biggest difference is the lack of curly braces.
Collections Are Awesome
It’s much lighter-weight to throw things into a list or map (dictionary in Python) than it is in Java. Get out of the mentality that you have to jump through import hoops and rigid notation to create, access, or return collections. In Python, they even let you do cool things like assign multiple variables as a return from a call:
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
Looping Feels Weird At First
I got a little thrown off by this initially. Most loops read really well:
for square in openSquares:
However, when looping through a set of numbers, you need to use the range method:
for i in range(0, 10):
Looking at both of those examples brings me to my next point…
Don’t Forget The Colon
This keeps throwing me off, but after declaring a function*, loop, or if statement, don’t forget to end the line with a colon. I’m happy to be rid of curly braces, but I get over-ambitious and forget the colon too.
Don’t Over-engineer Configuration
Depending on what you’re doing, you can likely just stuff configuration values into a script and import it (not needing to compile really is liberating in this respect). That’ll also give you the use of lists and maps by default. If you’re not reading between the lines I’ll spell it out: no need for XML-based configuration, which is one of the more evil trends in Java.
There are definitely more things I could mention; don’t take this to be the only lessons I’ve learned (any other hints/tips are appreciated). But I do want to avoid a mammoth blog post that causes readers to go into a zombie-like trance, so I’ll stop it here for now. I do want to thank Devan (dgoodwin) and Jesus (zeus) for dealing with the Java-veteran-turned-Python-noob and not finding a way to crash my chat client to avoid more questions.
* I haven’t seen a solid explanation of “Call them ‘functions’ because you’ll sound like a Java guy calling them ‘methods’”, but this feels like something where using the wrong term will make me stand out as a Java developer in a Python world. So I’ve been advised to take a militant approach of “Yes, I’m a Java guy learning Python, deal with the occasional terminology missteps.”


dgoodwin
January 13th, 2010 at 11:40 am
You forgot to include “bitch” at the end of that!
Glad you’re enjoying it. Coming from Java I sure found it refreshing to work with Python.
Michael DeHaan
January 13th, 2010 at 12:28 pm
Re: “Depending on what you’re doing, you can likely just stuff configuration values into a script and import it (not needing to compile really is liberating in this respect). That’ll also give you the use of lists and maps by default.”
Don’t do this, as it is a potential security hole. A much more awesome trick is using either yaml or simplejson and doing simplejson.loads() or yaml.loads(), etc. If you need INI style files, use ConfigParser (which doesn’t have said collections), but in cases where you are happy with a module, just go the extra step and use simplejson or YAML and you are home free.
I find yaml is /slightly/ better for human readable files, but it is slower.
Michael DeHaan
January 13th, 2010 at 12:29 pm
FWIW, I call them methods.
I also refuse to call ‘dict’ anything but ‘hash’.
Jens Knutson
January 13th, 2010 at 3:15 pm
Michael’s right about not using Python for config files. I personally find YAML considerably more readable, and far more useful for some tasks. That said, YAML can get a bit slow if you start building large or very files with it.
If you do play with YAML, make sure you install the ‘libyaml’ package in addition to PyYAML. Then, to get PyYAML’s to actually reap the speed boost from libyaml, you have to specify using it in the the .load() and .dump() methods, like this:
yaml.dump(data, Dumper=yaml.CDumper)
yaml.load(yaml_file, Loader=yaml.CLoader)
(Why the hell it can’t just f#$&ing autodetect is beyond me, but whatever.)
Hope that helps!
Jay
January 13th, 2010 at 3:29 pm
Awesome feedback on the config stuff, I really appreciate it. This is exactly why I posted about this stuff.
Thankfully, all of my code so far has largely just been learning, so I haven’t unleashed any massive configuration-related security holes.
Keith
January 13th, 2010 at 4:20 pm
range()?!?!?
no, no, no, no, no, if you’re going to do a numeric loop, you want xrange()
and you’ll start to wonder why they’re different, and you’ll start learning about generators, and realize that Python is really freaking awesome
Michael DeHaan
January 13th, 2010 at 8:33 pm
Keith — then if you look at Ruby, and see how much better blocks and generators are there.
Jens — I really thought it used to auto detect when I was using it. Perhaps this is newness.
Anyway, JSON is about 200x faster than Yaml in python, C library or no, because it doesn’t have to deal with ambiguity and the lexer (or what have you) is therefore much much simpler.
Simplejson on the other hand is somewhat stupid about converting things to unicode when you don’t expect it, so get used to checking isinstance(foo, basestr) versus seeing if something is a string, etc.
As you can tell, I spent WAAAAY too much time with serializers in Cobbler-timeframe.
Jay
January 15th, 2010 at 9:44 am
Wow, you weren’t kidding. I’ve only dorked with the JSON implementation so far, but it’s crazy easy.
simplejson.load(file(‘name’))
That ROCKS.