What does Python offer?

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu May 30 04:50:13 EDT 2002


"Solosnake" <solosnake at solosnake.without_this.freeserve.co.uk> wrote in
news:ad36sv$7h5$1 at newsg1.svr.pol.co.uk: 
> I was really enquiring about how python might differ from other
> langauges, and how this might be pertinant to games programming. e.g.
> I believe it is an interpreted langugae, as opposed to compiled? So AI
> code written in Python could be modified by end users? This cannot be
> done with C++ unless you expect the end user to compile his/her own
> dll. I do not wish to control or modify other games, I am a student of
> games programming, I would be using python in my own games projects.

You certainly could allow end users to add or change AI components by 
writing them in Python. This is one of the possible reasons for choosing 
Python for this task, but given that Startrek: Bridge Commander chose to 
use Python but not (AFAIK) allow end users to modify it, this cannot be the 
only answer.
The primary reasons for using Python that I can think of are:

Getting the product out of the door much sooner.

Flexibility. You can program so much more quickly that you can rewrite 
chunks of code using completely different algorithms if your original 
choice was wrong, and still have it complete sooner than if you had done it 
in C++.

It frees you up to think about what the code should do, rather than how to 
do it.

> I do not wish to invest any time in learning python if it is of no
> special use, and everything I could do could be done with C++ anyway.
> How does/can Python be integrated with other languages e.g. C++?
To integrate with C, the best way (IMHO) is to simply read the Python 
documentation on extending/embedding and the Python C/API. Look at the 
implementation of some of the modules that come with Python and build on 
that. I find this to be an easier and more flexible way than using SWIG but 
you might find differently. For C++ you might want to look at Boost/Python.

> 
> I don't want to write my games in python, unless it runs faster then
> C/C++/assembly? :)
You can pretty well guarantee that if you write it in Python it won't run 
faster than if you coded the same algorithms perfectly in C/C++. However, 
you would probably take around 10 times longer to write the same code in 
C/C++, and you might find it hard to measure the difference in runtime. For 
the sort of application you are asking about, almost all the CPU time is 
spent processing fancy graphics. The AI component usually takes a 
comparatively small amount of processor time. You may be trading off a 
small amount of performance for the benefit of getting the program to run 
faster, better, sooner, and with much more flexibility. Your choice.

If, when complete, you find your Python code is too slow, then usually you 
can optimise a small portion in a faster language to get most of the 
possible speedup. However you won't usually know until complete which 
portion needs to be optimised.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list