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.




Op 11 maart 2012 06:13 schreef Lee Buckingham
<[email protected]>het volgende:

> Just to add a little more to Julian's response:
>
> in a function, you can refer to a global variable without using the
> global keyword, like this:
>
> a = 1
>
> def something()
>     if a == 1:  print("sure does!")
>
> #later on
> something()
> # output prints "sure does!"
>
>
> On the other hand, you cannot **set it equal to something** without
> using the global keyword.  Python is designed to assume you are making
> a new, function-level variable that happens to have the same name.
>
> #this fails
>
> a = 1
>
> def something()
>     a = 2
>
> #later on
> something()
> print(a)
>
> #the output will say "1"
>
>
> This could have gone either way, I suppose.  The authors of python
> could have had the interpreter do it the way you are suggesting.   If
> I type "a=whatever", the interpreter could look up to the next scope,
> or to the highest (line level) scope, etc... and use any like-named
> variable first.  This could work.
>
> The problem is that in this situation, you would have to use something
> like a "local" keyword every time you did NOT want to worry about the
> possibility of accidentally naming a variable the same thing as some
> other variable in a higher scope, like the line level variables
> yourself and other coders prefer.
>
> As it turns out, the intent of the python designers favored the first
> way, so that they could relax a bit about variable names (a lot of us
> use variables named x, y, i, a, b, etc.., all the time, throughout the
> code).  However, for the times when we really want to look outside a
> function, just saying "global x" once, at the beginning of a function,
> works just fine.
>
> There really are debugging issues both ways.  I have beat myself
> senseless trying to figure out why my functions seem ineffective
> before finally (days later) realizing I just forgot a global
> statement.  I avoid this now, for the most part, by using a lot of
> classes and objects, which is just a preference on my part (and it
> drives my brother crazy).  From the other perspective, I can imagine a
> similar problem with your favored approach, where I get ghost-changes
> to variables on the game level for no obvious reason.  Days later,
> after bruising my forehead and keyboard, I might notice that I named a
> function level variable the same thing as a line level variable.  In
> my case, this would be much, much more frustrating, since I am not
> very creative with variable names.
>
> I really hope this helps a bit.  Python is a very friendly language,
> but like all languages, is sort of slanted in certain directions of
> its own.  I found myself repeatedly frustrated and irrate about the
> design of VB.NET, C#, and Java, for instance.  For me, python solved
> my problems because I think the same way.  There's nothing wrong with
> preferring one over the other; it's just our nature, I suppose.
>
> Good luck with your coding!
>
> All the best,
>
> -Lee-
>
> On Sat, Mar 10, 2012 at 8:31 PM, Julian Marchant <[email protected]> wrote:
> >
> > 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 = > 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 <[email protected]> wrote:
> >
> >> From: Brian Brown <[email protected]>
> >> Subject: Re: [pygame] Declaring variables in function as if at code's
> line-level
> >> To: [email protected]
> >> 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 <[email protected]>
> >> 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 <[email protected]>
> >> 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 <[email protected]>
> >> 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 <[email protected]>
> >> wrote:
> >> >> That is not true, Chris.
> >> >>
> >> >> On 3/10/12, Christopher Arndt <[email protected]>
> >> 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
> >> >
> >> >
> >>
> >
>

Reply via email to