Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 24th, 2007, 10:00 PM   #1
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
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!"
MR.T is offline   Reply With Quote
Old Jan 24th, 2007, 10:20 PM   #2
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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
Game_Ender is offline   Reply With Quote
Old Jan 25th, 2007, 4:10 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by MR.T View Post
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.
Arevos is offline   Reply With Quote
Old Jan 25th, 2007, 10:34 PM   #4
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
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!"
MR.T is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:09 AM.

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