[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
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] Declaring variables in function as if at code's line-level
- From: Julian Marchant <onpon4@xxxxxxxxx>
- Date: Sat, 10 Mar 2012 20:31:31 -0800 (PST)
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sat, 10 Mar 2012 23:33:40 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1331440291; bh=8mjjP173AewgNP0r9tn8nNlM5g3FE7EhyT+neo1NojQ=; h=X-YMail-OSG:Received:X-Mailer:Message-ID:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=lcWlr5aLFIOZllq4lJWjowDpx99XeZI1g2pqhgoi4ELhK25QCzBwW8+FkibKVFONG0JlrJqXBZu7bu13rZPoJRi1duH3YOGu2mzgV05wUENAU3M2A0YPCybDzTma5IYkGCXIsi/jFb5ok+sUhQqJxRC0Ng23AZ/J6Rj8XnVTMw8=
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Message-ID:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=bCXZDVqx7ACkZgPv87p6QX5PrMkmnF8OMO10YOGweC/8qples/jfH8fcNPC2/2EZFREe/Kh+2HWIS587Povz5W/EuZ0BMPeOnr1hZB73lWoDKmy4Es1mfW5LT9++9G51C2GvDXkQg5xjpuDsjEu35/u5us+ensO88fKrsn3kQ1c=;
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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
> >
> >
>