Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Some general programming questions in python (http://www.programmingforums.org/showthread.php?t=12438)

MR.T Jan 24th, 2007 11:00 PM

Some general programming questions in python
 
I have a few general programming questions. But since there isn't a general programming section, and I'm using python, I figured I'd post them here.

First off, say my program is a game. The user is asked to and selects defend. In order to defend, a variable is changed to true. Which is of course, a single line of code. Does it make difference performance or design wise, if that line is in the input function that asked the player to defend? or if it's in its very own defend function?

Now on to classes. I understand how classes are used for inheritance, polymorphisms and so on. What I never understood is, are they suppose to be used for organization also? Even if a function or something, doesn't really need to be in a class, should it be?
Also, should I put my variables that are used by multiple function, inside a class that they are at least somewhat related to, so I can use there scope to change them, instead of making them global? Would that be more efficient?

And lastly, should comments that are related to something like a variable be placed beside it or above it? And for multi-line comments, should I use triple quotes, or a # sign at the begging of each line?

Game_Ender Jan 24th, 2007 11:20 PM

Function Calls: Function call overhead is quite high in python compared to compiled to languages like C or C++. Yet this really does not matter until you start to hit performance problems. Functions let you reuse code that you would other wise have to write twice, and they let you clarify complicated tasks by breaking it into manageable junks.

Classes and Globals:
There are very few reasons for globals, just make them class members. I use classes for organization. In my current project, a 3D simulation, I have classes devoted to physics, graphics, input and so on. I am sure others will be able to give you a better ideas/recommendations.

Comments:
See the python style guide line.

Arevos Jan 25th, 2007 5:10 AM

Quote:

Originally Posted by MR.T (Post 123074)
Also, should I put my variables that are used by multiple function, inside a class that they are at least somewhat related to, so I can use there scope to change them, instead of making them global? Would that be more efficient?

It's not about being efficient, per se. It's more about flexibility. You can only have one set of global variables, but you can potentially have more than one object of a certain type.

For instance, say I was designing a single player game, and created certain global variables for health, player position, etc. This would work okay, but then what if you wanted to add an extra player? If all your variables were in a Player class, it would simply be a case of instantiating a new Player.

Or say you had a global variable holding a database connection. What happens if your database is then divided up into two for efficiency reasons? Again, having a object you pass around is more flexible than a single global variable.

Generally speaking, global variables should be used as little as possible, as they're more inflexible than variables on objects. It also helps organise your program more cleanly.

Other than that, Game_Ender is right on the money. And don't worry about efficiency of function calls - if you get to the point where it matters, you probably should be farming out some functionality to a binary library in C or something anyway.

MR.T Jan 25th, 2007 11:34 PM

Thanks for all the great information guys. You have helped me a lot.


All times are GMT -5. The time now is 1:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC