[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



I am struggling with my desire to say things that would be insulting to you.

Listen closely: you CAN read global-level variables at any time as long as there isn't a local variable with the same name. You CAN write to global variables from within a function as long as you first declare it within that function with the ``global`` keyword. You CANNOT write to global variables without using ``global``. The reason for this is simple: if you don't use ``global``, the interpreter assumes that you are assigning to a local (function-level) variable.

Frankly, I don't see what the big deal is. It's just ONE extra line per funtion per variable that you have to write to. Consider:

#!/usr/bin/env python

a = 0
b = 0
c = 0

def foo():
    global a

    a += 1
    if a > b:
        a = 0

def bar():
    global b
    global c

    b = a + 10
    c += a

    if c > a + b:
        c = 0

See how easy that is? You're making a big fuss over nothing.

--- On Sun, 3/11/12, Brian Brown <brobab@xxxxxxxxx> wrote:

> From: Brian Brown <brobab@xxxxxxxxx>
> Subject: Re: [pygame] Declaring variables in function as if at code's line-level
> To: pygame-users@xxxxxxxx
> Date: Sunday, March 11, 2012, 12:15 AM
> "line-level" is leftmost of the
> source code.
> (I thought my example code would clarify that.)
> My question is very clear, I think, I'm not sure why you
> guys are
> having so much difficulty understanding it.
> Sorry about that. I'm not trying to frustrate you, I just
> wish people
> would answer my questions.
> (I rarely get good answers on these mailing lists. I guess I
> made a
> mistake asking here.)
> Haven't I just given you one of the most profound statements
> of
> efficient game programming?
> Shouldn't you be grateful? Thank you for all your replies
> everyone I
> guess I'm not wanted here.
> 
> 
> On 3/10/12, Brian Brown <brobab@xxxxxxxxx>
> wrote:
> > That is not true, Ryan. I am currently making a game
> with Python and
> > Pygame, and my question is directly related with its
> development.
> >
> > On 3/10/12, Ryan Hope <rmh3093@xxxxxxxxx>
> wrote:
> > Why is this even be talked about on the pygame ml?
> This has nothing to
> > do with pygame.
> >
> > On Sat, Mar 10, 2012 at 6:30 PM, Brian Brown <brobab@xxxxxxxxx>
> wrote:
> >> Hi pygame users, just a simple question-- How
> can one cause variables
> >> at "function-level" to behave like variables at
> "line-level"? (With
> >> basic python code) I just want to avoid using
> "global" over and over
> >> again (in many different functions) while I
> want to declare, use, and
> >> delete all my game's variables inside
> functions.Thanks.
> >>
> >> It should make my program very simple and
> straight-forward if I could
> >> do this. (As I have explained in the previous
> replies to this thread)
> >> I would like to know how it can be done--
> without immature,
> >> unproductive statements like:
> >> "Don't freak out at the fact that I used the
> "class" keyword."
> >> Thank you.
> >>
> >> Matt
> >>
> >> On 3/10/12, Brian Brown <brobab@xxxxxxxxx>
> wrote:
> >> That is not true, Chris.
> >>
> >> On 3/10/12, Christopher Arndt <chris@xxxxxxxxxxxxx>
> wrote:
> >>> On 10.03.2012 23:35, Christopher Night
> wrote:
> >>>     DO:
> >>>     * Access variables.
> >>>       (Move game according to
> current-variable-status and
> >>> player-input)
> >>>
> >>>     * Output to graphics and
> sound card.
> >>>       (Display game according to
> current-variable-status.)
> >>>     LOOP
> >>>
> >>>     That's really all we need.
> >>>
> >>> Who's this "we"? Certainly doesn't
> include me, because I need *a lot*
> >>> more from a programming language.
> >>>
> >>> Brian, I think you should read a bit
> about namespaces and why they are
> >>> a
> >>> good thing. You won't get very far with
> Python with your point of view
> >>> -
> >>> or in any other programming language
> for that matter (except maybe PHP
> >>> -
> >>> just kidding ;) ).
> >>>
> >>>
> >>> Chris
> >>>
> >>
> >
> >
> >
> > --
> > Ryan Hope, M.S.
> > CogWorks Lab
> > Department of Cognitive Science
> > Rensselaer Polytechnic Institute
> >
> >
>