Sign in to follow this  
biggjoee5790

Python.. more than scripting??

Recommended Posts

Hey im just posting this to get a feel for how people feel about Python being used as a standalone programming language as opposed to using it as a scripting language for certain portions of programs? Im mainly talking about 2d and 3d video games, but how do you feel about it being used as the sole language to program various different types of software? Is it powerful enough to do more than script?

Share this post


Link to post
Share on other sites
Python wasn't designed as an extension language. In fact, when you try to use CPython for extension purposes, you run into a number of difficulties and inconveniences. The language was intended to serve as a standalone programming environment, extensible in C; that it is embeddable is a happy side effect.

In other words, yes, it is powerful enough to do more than script. There are all manner of libraries to provide extensive capabilities to Python, from multimedia libraries to full 3D engines.

Enjoy!

Share this post


Link to post
Share on other sites
Thanks for the info.. So let me get an idea of its capabilities please. Lets say you knew both Python and C++ extensively. You then took an entirely C++ written sourcecode from a game like.. lets say.. half life 2. Now, If you had access to all the 3d models and maps and all that stuff, could you successfully rewrite the entire game in python? Im guessing you would have to of course use python ready APIs such as pyopengl instead of tools that are only made for C++ like DX or OpenGL. So is something like this possible? What would the outcome be? Im obviously not going to do this lol but its just an example to show me what its capable of.

Share this post


Link to post
Share on other sites
I'm currently learning Python through building little apps - currently text-based games similar to roguelikes (I'll be posting about this in my journal as I go). I'm evaluating it to see how well it suits my purposes as a general programming language; if it works well I'm planning on moving mainly to Python. At the moment I'm more concerned with development time than how fast the program runs.

As Oluseyi wrote, Python is designed as a stand alone programming language with the capability of writing your own libraries in C. My general plan when I get to speed critical apps like games would be to build the thing in Python first, profile it to see where the bottlenecks are (the bits of code that are executed the most), and rewrite those bits in C. If you build your program like that, I could see your Python with C program being almost as fast as a pure C one, with the added benefit of being much easier to write.

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
What would the outcome be?

It would be marginally slower, but that's about it. The Senshi was telling me during GDC about his game that uses PyOpenGL and PyODE, and how it's surprisingly performant as well as flexible and robust - I thought there might be some slow downs with graphics operations, but he said that's not the case.

Share this post


Link to post
Share on other sites
Ya I read a lot obout people writing in Python and using C to speed up certain portions.. im not concerned with that now anyways since I only know Python :) Also when you say it would be marginally slower, what do you mean by that? Like the game will run at low fps?

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
Also when you say it would be marginally slower, what do you mean by that? Like the game will run at low fps?

In most cases it wont run noticeably slower, unless you are doing a lot of heavy lifting directly in . For most games, the graphics will be running through an already optimised C/C++ API (OpenGL or DirectX), and physics will also be implemented in C/C++, so most of the python code is high-level logic, AI and glue code — not typically the most expensive parts of a game.

Share this post


Link to post
Share on other sites
so if you use pyopengl and pyode, you dont need any C++ knowledge do you? Do you just have to learn how to access their functions through the python language instead of C++?

Share this post


Link to post
Share on other sites
It would be helpful to know a little bit about C/C++ syntax because people don't typically rewrite the complete API documentation with Python in mind. At some point you'll probably have to read docs that give C++ examples of usage and work out for yourself how it should look in Python.

Share this post


Link to post
Share on other sites
It's valuable to know C++. It will remain valuable to know C++ for a long time to come. However, it is not required to know C++ to create a reasonably complex, performant game.

Re marginally slower: some semantically equivalent operations in Python take a little bit longer to complete than their C++ counterparts because the Python version does more. This is due to the nature of the language - the biggest performance hit is in dynamic type resolution, which isn't used in the majority of Python statements but is insanely valuable when you do need it. A pretty cool project I discovered a while back is StarKiller, which removed the dynamism from objects that it can statically determine don't need it. Unfortunately it appears dormant since the 2004 PyCON presentation, and there's been no attempt at integrating it with any production distribution.

Anyway, the areas where this dynamism constitutes a relevant performance penalty are very few. Generally, doing the semantic equivalent in C++ requires more code, anyway, so using Python turns out not to be much slower in practice. What this means for your game is that you can do slightly less each frame - push a few less polygons, apply a few less effects - while maintaining the same frame rate (or, conversely, generate a few less frames per second). It likely won't be a bottleneck for you for quite a while, anyway, so don't worry about it.

Share this post


Link to post
Share on other sites
to be honest, my python code has run faster than my C code. It's a byproduct of 2 things, first me being a lousy C programmer, and secondly python code is much easier to read after I've written it, so I'm ten times as likely to revise it, and I find the more I revise my code, the faster it goes, and while my python games start off slower than my C ones, they almost always end up faster in the end.

Share this post


Link to post
Share on other sites
Thanks for the info guys. Im definetely going to learn C++ after i have really gotten good with python. After I feel comfortable with python and I begin making 2d and 3d games (simple ones nothing too crazy) Im going to dive into C++, which im sure will be easier to pick up after knowing python very well

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
A pretty cool project I discovered a while back is StarKiller, which removed the dynamism from objects that it can statically determine don't need it. Unfortunately it appears dormant since the 2004 PyCON presentation, and there's been no attempt at integrating it with any production distribution.


Maybe that project has been superseded by Psyco? I think Psyco does the same kind of optimization (as well as some others), and it's pretty popular.

Share this post


Link to post
Share on other sites
Quote:
Original post by pinacolada
Quote:
Original post by Oluseyi
...StarKiller...

Maybe that project has been superseded by Psyco? I think Psyco does the same kind of optimization (as well as some others), and it's pretty popular.

No, they're seperate projects. Psyco shares an/the same author with PyPy, who talked about using StarKiller's methods on the call graphs that PyPy already generates.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by biggjoee5790
Also when you say it would be marginally slower, what do you mean by that? Like the game will run at low fps?

In most cases it wont run noticeably slower, unless you are doing a lot of heavy lifting directly in . For most games, the graphics will be running through an already optimised C/C++ API (OpenGL or DirectX), and physics will also be implemented in C/C++, so most of the python code is high-level logic, AI and glue code — not typically the most expensive parts of a game.


Sadly, this is just untrue.
Python is more than marginally slower than C++, graphics are much more than just OpenGL doing it all, games are more than just graphics and physics.

So your game can be a little game that does not demand that much CPU power, and Python would be perfectly suited for this ( most 2D casuals games falls into this category ).
Or you can have Python and C++ working together, with embedded or extended Python, and C++ doing most of the CPU intensive jobs.

For any modern commercial-quality 3D game, you will need some C++ involved.

Emmanuel

[Edited by - Emmanuel77 on March 2, 2008 6:57:52 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel77
Quote:
Original post by swiftcoder
Quote:
Original post by biggjoee5790
Also when you say it would be marginally slower, what do you mean by that? Like the game will run at low fps?

In most cases it wont run noticeably slower, unless you are doing a lot of heavy lifting directly in . For most games, the graphics will be running through an already optimised C/C++ API (OpenGL or DirectX), and physics will also be implemented in C/C++, so most of the python code is high-level logic, AI and glue code — not typically the most expensive parts of a game.


Sadly, this is just untrue.
Python is more than marginally slower than C++, graphics are much more than just OpenGL doing it all, games are more than just graphics and physics.

So your game can be a little game that does not demand that much CPU power, and Python would be perfectly suited for this ( most 2D casuals games falls into this category ).
Or you can have Python and C++ working together, with embedded or extended Python, and C++ doing most of the CPU intensive jobs.

For any modern commercial-quality 3D game, you will need some C++ involved.

Emmanuel


haha well thats a completely different opinion lol.. i wonder what the reality is lol :) I guess Ill just find out myself when it comes time

Share this post


Link to post
Share on other sites
What are your opinions on learning C#? How does that compare to C++ and Python? Is it more suitable for games than python? if so, is it reasonable for a beginner to start with as a first language?

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
What are your opinions on learning C#? How does that compare to C++ and Python? Is it more suitable for games than python? if so, is it reasonable for a beginner to start with as a first language?

Here's the single most important piece of advice you will receive on this issue: it doesn't matter which language you pick.

What matters is that you pick a language, stick with it, and continue working in it until you have a solid grasp of the fundamentals of programming, which are independent of any particular language. There are no bests, just goods. Python is good for beginners. C# is also good for beginners (depending on your perspective, maybe slightly less good, but still good). C++ is not so good for beginners, but a determined beginner starting with C++ will turn out just fine.

Pick a language. Stick with it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel77
So your game can be a little game that does not demand that much CPU power, and Python would be perfectly suited for this ( most 2D casuals games falls into this category ).

I take a number of exceptions to your post. First, the contention that Python is "more than marginally slower than C++" - on what basis? For equivalent source-level instructions, or for equivalent semantic instructions? For what types of instruction complexity? For instance, integer addition in C++ is much faster than in Python because of the need to resolve the late-bound identifier, but dynamic dispatch is much closer because this is the nature of all Python method dispatch. Without context, the statement is meaningless.

Second, the assertion that Python is only good for "little games" that "do not demand that much CPU power" - what precludes Python from demanding extensive CPU power, and employing it effectively? Given a robust and optimized extension module for known bottlenecks (identified by profiling, not making blanket assertions), core application architecture can be defined in Python and proven not to degrade significantly in performance terms. This is precisely the data - anecdotal, perhaps, but better than unsupported assertions - TheSenshi gave me re his experience developing a full-blown 3D game in Python.

High-performance game development in Python ultimately requires the employ of C++ for extension modules, certainly, but a surprising amount of the game itself can be written in Python.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Second, the assertion that Python is only good for "little games" that "do not demand that much CPU power"


If that's true someone should email the Eve Online guys quick before their (stackless) python powered servers fall over!

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by biggjoee5790
What are your opinions on learning C#? How does that compare to C++ and Python? Is it more suitable for games than python? if so, is it reasonable for a beginner to start with as a first language?

Here's the single most important piece of advice you will receive on this issue: it doesn't matter which language you pick.

What matters is that you pick a language, stick with it, and continue working in it until you have a solid grasp of the fundamentals of programming, which are independent of any particular language. There are no bests, just goods. Python is good for beginners. C# is also good for beginners (depending on your perspective, maybe slightly less good, but still good). C++ is not so good for beginners, but a determined beginner starting with C++ will turn out just fine.

Pick a language. Stick with it.


yea man i have realized that. So you know I am really progressing well with Python finally :) I did look at C# because it seems like a great language to program games with,(having XNA and all that) but after lookin at some code it looks awfully similar to C++.. and I wanna stay away from that sort of syntax until i know python really well.. its just more clear. One problem I am encountering in my learning is figuring out how to create the program I want. Like I know all the syntax and functions and stuff really well. I just have trouble putting it all together and creating stuff. Like If you told me to make a program that does a certain thing... I would know in my head what to do, but getting it to flow in a program kills me.. I guess the only way to solve this is to keep writing programs right? it seems like this sorta thiung will get better with practice. Because the memorization of syntax comes really easy to me

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel77
Quote:
Original post by swiftcoder
In most cases it wont run noticeably slower, unless you are doing a lot of heavy lifting directly in . For most games, the graphics will be running through an already optimised C/C++ API (OpenGL or DirectX), and physics will also be implemented in C/C++, so most of the python code is high-level logic, AI and glue code — not typically the most expensive parts of a game.


Sadly, this is just untrue.
Python is more than marginally slower than C++, graphics are much more than just OpenGL doing it all, games are more than just graphics and physics.


It isn't quite as simple as that. Sure python is a little slower than well written C/C++, but lets take a look at a single player FPS (such as Halo). There are a few subsystems that are far-and-away the most expensive:

  • Graphics: This is the major cost, but with techniques such as hardware skinning, upwards of 90% of this happens either on the GPU, leaving your CPU free to do other things, or in the low-level API, which is already implemented in C/C++.

  • Physics: This is another major cost, but few games write their own physics engine, so this will be off the shelf, and written in heavily optimised C/C++.

  • AI: This is typically implemented in a scripting language anyway, because it is greatly easier, and in some cases may perform better due to the dynamic nature of python.

  • High-level Logic: This is another area where dynamic languages have a distinct advantage. If you have ever used a scripting language to write glue code or GUi bindings, you will be well aware that many of the techniques needed are trivial in python, whereas they are very complicated in C/C++.



So we have a game that if well thought out performs only a little worse than the C/C++ game, but also can be developed in far less time, and with far less trouble.

And if we are dealing with a networked game, such as an MMO, then the savings become even more important. It would take monumental effort to create a framework in C++ that offers a fraction of the power and expressiveness of Twisted, and I don't see many reports of Twisted being a performance hog...

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
High-performance game development in Python ultimately requires the employ of C++ for extension modules, certainly, but a surprising amount of the game itself can be written in Python.


Don't get me wrong, this is basically what I was saying.
I'm just saying you can't make a real comercial quality and technical game with only Python. And that was the original poster question.
When you're dealing with _lots_ of data to process, Python is just slower, and way slower. And big games are about processing a lot of data (polygons, objects, waypoints, particles, ai states, ...)

By no way, I am saying you can't use Python as a higher level language, using some C++ modules at some point.
By the way, for my hobby projects ( on PC, and I'm trying it on DS ), this is precisely what I'm using : a engine with embedded Python, and I'm quite happy with it. It let me make some prototypes really quickly...

On this subject, I have seen several projects in my compagny where the high level logic was done in script languages ( custom one, UnrealScript and LUA ), and where it became a bottleneck by the end of the project. Simply because of the insanely huge quantity of scripts that where written. And switching some parts from script to C in a game in advanced state is by no way in easy thing. In the case of the game I was working on ( using a custom scritp ), we developped an automatic script to c tool. And the gain was really important.

Quote:
Original post by phantom
If that's true someone should email the Eve Online guys quick before their (stackless) python powered servers fall over!

Eve Online is by no way done with python as their sole language.
By the way, there was on this forum a really interesting note on python in Eve Online by Kylotan, where he said they basically the CPU work is balanced between servers, so any CPU issue could be sorted out by adding some servers, which is quite cheap ( there are only here for CPU, and don't increase the bandwidth )


One last point, although I really think Python as a language is superior to C#, the tools available with C# are really superior to what you can get with Python.
For any beginner, I would really advice using C# and XNA. ( and it costs me to tell that).

Share this post


Link to post
Share on other sites
Ya it does seem to me that C# has way better tools. And when coupled with XNA it seems like a really great language to make games in.. even serious commercial quality stuff. But the point was made about sticking to a language... and thats what Im gonna do lol. Sure I can stop Python and start C# but is it really worth it when Im starting to get comfortable in Python? Im far from good but I know alot of the syntax.. i just have to start writing tons of programs so that it becomes second nature to me. I think Im just gonna keep at it. Anyways my LEARNING PYTHON 3rd EDITION came today lol.. I love gettin new books. Next Im gonna try and pick up a good book about game programming in 2d and 3d.. but not for a while.. at least until i feel sure about my skills. Does anyone have suggestions on how to practice? Like Im trying to get to the point when I can easily get the idea from my head into the code. I just have so much trouble structuring. Like for instance If I wanna make a tictactoe game. I know I need to display a board, update the board, etc, etc, but I really cant put all of this together into a program. But on the other hand if someone said make a list that represents a board, or create constants that represent player pieces... i can easily do that because I know the syntax pretty well, its just a matter of putting it to use. Ok now Im rambling.... :)

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
I would know in my head what to do, but getting it to flow in a program kills me.. I guess the only way to solve this is to keep writing programs right? it seems like this sorta thiung will get better with practice.
Yes, that's the sort of thing you can really only get a firm grasp on through practice. As you gain experience you will start to learn what does and doesn't (don't be afraid of mistakes, you'll make a lot while learning and they're a great way to learn!) and gradually build up a better idea of how to tackle various different types of problems.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this