![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
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?
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!" |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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.
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#3 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
Thanks for all the great information guys. You have helped me a lot.
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!" |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Python programming game? | commodore | Python | 7 | Feb 7th, 2006 6:26 PM |
| [tutorial] Python for programming beginners | coldDeath | Python | 30 | Dec 14th, 2005 11:35 AM |
| Python - A Programmers Introduction | coldDeath | Python | 17 | Aug 19th, 2005 12:41 PM |
| Emergency: Confusing C Programming assignment questions | silvia | C | 3 | Jul 13th, 2005 3:39 AM |
| n00b questions on Win Programming and MFC | astinus | C++ | 6 | Apr 23rd, 2005 10:50 PM |