[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Declaring variables in function as if at code's line-level



Thanks for the replies everyone.
I guess Python just doesn't have a "declare-variable-as-global-for-whole-module" feature.

(I don't generally have problems with variable overlap because I'm very careful with naming my global variables.)

(I just remembered though-- for some reason,
list-type variables' individual values/items  can be easily accessed throughout the whole module even if:  it was just "global"ed once inside the function it was created.)

But I just wish I could create variables (of ANY TYPE) which are universally acknowledged by the WHOLE module.

Many of my variables' names are re-stating the exact value they hold, so I rarely have any confliction with names:

filename_of_current_level = 'desert.txt''
Font__New_Times_Roman_size_20 = ...
pi_divided_by_180 = math.pi() / 180

etc.
The names are just according to what values they hold which-- are all unique anyways!
And when the number of variables start reaching the hundreds, it just gets really crazy with "global this global that" ALL OVER my code, again and again . . .
: (
I have to keep remembering to global every other variable in any function that uses my main game variables.

If I use dictionaries, I must type:  "d['whatever']" for ALL my game variables!!!
Same with using classes--  "c.whatever"

It's like    "pygame.draw.line()"   instead of just: "DRAW_LINE()"

or like: "random.random()"   instead of just:
"RANDOM_NUMBER_BETWEEN_0_AND_1()"

"Pygame dot draw dot line" (It sounds so lame too) 


Arrg!! It's like driving me crazy!
 . . . 
sigh
. . .

Sorry guys, I'm just feeling very frustrated with all these crazy programming conventions.
I think I need to take a break from programming to help me cool off . . .

By far Python and Pygame are the easiest game development tools I know so-- I have no other choice-- Redundant "global"s wreaking havoc on every corner of my program will have to do . . .

Anyways,
Sorry about my previous posts, sometimes when I get too involved in programming I get a little short tempered-- somehow. .
(By the way, thanks for not insulting me even though I deserved it.)

I also re-read the messages and found that I had a few inaccuracies and typos as well.

I need to remember that people are far more important than just some silly game . . .

Thanks again everyone for all your replies. : )
I really appreciate it. ; )

(I'll go get some sun and fresh air hopefully that will help me "cool-off")

Matthew N. Brown

(PS in case you haven't already, you can check out my really cool physics program in the Python Draw docs! I'm not a "troll" I was just having way too much fun. lol)

On Sun, Mar 11, 2012 at 11:44 AM, Lenard Lindstrom <len-l@xxxxxxxxx> wrote:
Hi,

To answer Nick's, question: yes. Anything in a module's global namespace is directly visible from a function declared within the module, unless hidden by a function argument or local variable having the same name. Even then, you can use the globals function to access the global names.

And yes Lee, you have it right. The Python compiler would notice the assignment to font with function something and therefore create a function local variable that hides the global font variable. Of course, the global font can still be accessed directly through the module's global dictionary:

from game import font


def something():
     font = 1
     globals()['font'].render("or other")  # call imported global font function
     print font  # print local font value: "1"


On 10/03/12 09:45 PM, Lee Buckingham wrote:
I believe so.  Just don't say something like:

def something():
     font = 1

...not that you would, but if for whatever reason it happened, you
would have created a fuction variable called 'font'  and the global
namespace would be ignored because of it.

(Do I have this right, guys?)


-Lee-



On Sat, Mar 10, 2012 at 9:34 PM, Nick Arnoeyts<nickarnoeyts@gmail.com>  wrote:
I have a little additional question about the explanation Lee gave.

Is it possible to use class instances from the global namespace in a
function? For example:

from game import screen, font

def get_text(string):
    font.render(string) # parts cut out because I'm in a hurry

I figure this is probably possible, but it can't hurt to ask for future
reference.