1

Possible Duplicate:
Common Pitfalls in Python

I'm learning Python and I come from a diverse background of programming languages. In the last five years, I've written quite a bit of Java, C++, VB.Net, and PHP. As many of you might agree, once you learn one programming language, learning another is just a mater of learning the differences in syntax and best practices.

Coming down from PHP, I've become very accustomed to a lot of script-style language features. For instance, stuff like this tickles me insides:

# Retrieve the value from the cache; otherwise redownload.
if(!($value = $cache->get($key)))
    # Redownload the value and store in the cache.
    $cache->set($key, $value = redownload($key));

Python, though, doesn't consider assignment to be an expression. OTOH, it does support nice things like the in construct, which I find to be one of the greatest inventions of all time. x in y is so much nicer than !empty($y[$x]).

What other nuances, "missing" features, and performance bottlenecks should I watch out for? I'm hoping to make as seamless a transition into Python development as possible, and hope to learn some of the secrets that will help to smooth out development time and eliminate trial and error. Your insight is appreciated!

Community
  • 1
  • 1
mattbasta
  • 13,492
  • 9
  • 47
  • 68
  • 3
    This should be marked community wiki, I think. – detly Jul 12 '10 at 08:06
  • 1
    dupe: http://stackoverflow.com/questions/1011431/common-pitfalls-in-python – SilentGhost Jul 12 '10 at 09:11
  • I'm getting a pretty negative vibe from this question. Why should you view the situation as about finding out the "deficiencies" of Python? Does the fact that php does not support something like `if 1 – MAK Jul 12 '10 at 11:39
  • @MAK: Not so much that I'm looking for deficiencies as I'm looking for intricacies that only exist in Python. I'm all for learning the new things, but I don't want to get bitten in the ass on company time :) – mattbasta Jul 12 '10 at 15:14
  • Fair enough. It would be nice to update your question to reflect this. – MAK Jul 12 '10 at 15:25

5 Answers5

4

This one took me a few hours to figure out when I first encountered it in a real program:

A default argument to a function is a mutable, static value.

def foo(bar = []):
  bar.append(1)
  print(bar)

foo()
foo()

This will print

[1]
[1, 1]
Thomas
  • 174,939
  • 50
  • 355
  • 478
3

For your example, the usual way would be something like this

try:
    value = cache[key]
except KeyError:
    value = cache[key] = redownload(key)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
2

Threads do not do what you think they do, and probably shouldn't be used how you're used to using them. This is a huge gotcha for many people, especially for those used to Java where the custom is to subclass Thread implement the Runnable interface to do asynchronous work, and where there is language support for running threads in parallel (on machines with multiple CPU cores).

In general you probably don't want threads at all, but subprocesses. See my answer to the question "python threading and performace?".

(In more general, there might be a better way altogether.)

Community
  • 1
  • 1
detly
  • 29,332
  • 18
  • 93
  • 152
  • "especially for those used to Java where the custom is to subclass Thread to do asynchronous work" - Seriously? Who would do such a thing?? – perp Jul 12 '10 at 09:13
1

Exceptions are your friend.

In contrast with languages like C and PHP which use the return value to indicate errors, Python uses exceptions to interrupt the program instead of allowing the errors to cause further problems down the line.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Pythonic code is usually much faster than C-like code.

Something like:

new_list=[]
for i in xrange(len(old_list)):
   new_list.append(some_function(old_list[i]))

is better written as:

new_list=[some_function(x) for x in old_list]

or

new_list=map(some_function,old_list)
MAK
  • 26,140
  • 11
  • 55
  • 86