[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] squeezing a little fun out of java ... meh



On Tue, Jan 3, 2012 at 12:52 PM, Ian Mallett <geometrian@xxxxxxxxx> wrote:
I suppose you could do things like:
def my_func(in):
    if type(in) != type(""): raise ValueError()
. . . which emulate static types, but is still not a compile-time error.  In my early Python libraries, I found I was doing something of this sort because I wanted it to be robust, even at the cost of performance (this is a case where C++ wins: C++ is plenty fast, you don't have to do this particular kind of error checking, and you can put #ifdef/#endif around error-checking sections so that the code isn't even there when you don't want it).

That's good, I recommend this simple method for small-to-medium projects. The difference between dynamic and compile-time errors is minor when your compile times are almost instantaneous. You can get most of the benefit you would have gotten with static typing pretty easily with this method.

Just keep in mind that you usually *don't* want static typing. Don't go crazy and check the type of every single argument, just things for which only one type will do. Often numbers or Surfaces will fall into this category. Probably the most common issue that static typing would have helped me with when writing pygame games is simply getting the arguments to a function call out of order.

As for performance, just make it an assert which you disable (with -O) when you distribute the game.

-Christopher