Python Gotchas

What is a “gotcha”?The word “gotcha” started out as the expression “Got you!” This is something that someone who speaks idiomatic American English might say when he succeeds in playing a trick or prank on someone else. “I really got you with that trick!”

The expression “Got you!” is pronounced “Got ya!” or “Got cha!”.

Among computer programmers, a “gotcha” has become a term for a feature of a programming language that is likely to play tricks on you to display behavior that is different than what you expect.

Just as a fly or a mosquito can “bite” you, we say that a gotcha can “bite” you.

About this Page

This is a page devoted to Python “gotchas”. Python is a very clean and intuitive language, so it hasn’t got many gotchas, but it still has a few that often bite beginning Python programmers. My hope is that if you are warned in advance about these gotchas, you won’t be bit quite so hard!

Note that a gotcha isn’t necessarily a problem in the language itself. Rather, it is a situation in which there is a mismatch between the programmer’s expections of how the language will work, and the way the language actually does work. Often, the source of a gotcha lies not in the language, but in the programmer. Part of what creates a programmer’s expectations is his own personal background. A programmer with a Windows or mainframe background, or a background in COBOL or the Algol-based family of languages (PL/1, Pascal, etc.), is especially prone to experiencing gotchas in Python, a language that evolved in a Unix environment and incorporates a number of conventions of the C family of programming languages (C, C++, Java).

If you’re such a programmer, don’t worry. There aren’t many Python gotchas. Keep learning Python. It is a great language, and you’ll soon come to love it.

Other posts about Python Gotchas

Lists of Python Gotchas

This entry was posted in Python gotchas and tagged . Bookmark the permalink.

4 Responses to Python Gotchas

  1. glen worstell says:

    Another gotcha:

    i = 010        # gives i the value of 8
    j = int('010') # gives j the value of 10

    The first is horrid (copied from c), but the combination is super-horrid.

  2. Robert says:

    I’m not sure why the above commenter has such an abhorrence for octal notation. It’s in Python because it’s in C, and it’s in C because it’s used all over the place in UNIX (look up ‘octal’ on wikipedia if you’re interested).

    Octal notation is only used by Python when the programmer starts a literal number with 0, because there has to be some way to specify an octal literal, and starting with 0 is the tried-and-true notation. If you don’t want your numbers to be in base-8, don’t start your literals with 0.

    Octal isn’t used when converting from a string because of filler formatting. e.g. Starting employee IDs with many 0’s because you want to have a fixed ID length, but don’t have 100000 employee’s yet. No one would want those employee IDs to be converted to octal, so that’s not how the int() function works.

    • Alex says:

      In fact, the reason why int(‘010’) returns 10, and not 8, is because the int() function has a ‘base’ parameter which defaults to 10. So

      int('010', 8 ) returns 8
      int('010', 2 ) returns 2
      int('012', 3 ) returns 5

  3. Its like you read my mind! You seem to know a lot
    about this, like you wrote the book in it or something. I think that you can
    do with some pics to drive the message home a bit, but other than that, this is wonderful blog.
    An excellent read. I will certainly be back.

Comments are closed.