[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



Yeah, okay. Just to be clear, you're no longer asking for advice at this point, you're just giving your opinion of "flaws" that python has, right? You understand the situation, right?

-Christopher

On Sat, Mar 10, 2012 at 4:58 PM, Brian Brown <brobab@xxxxxxxxx> wrote:
#example 1:

variable_declared_at_line_level1 = 1
variable_declared_at_line_level2 = 2

def a():
     print variable_declared_at_line_level1
     print variable_declared_at_line_level2

def b():
     print variable_declared_at_line_level1
     print variable_declared_at_line_level2

a()
b()
>>> 1
>>> 2
>>> 1
>>> 2

#example 2:

def init_game_variables():
     global variable_declared_in_function1
     global variable_declared_in_function2
     variable_declared_in_function1 = 1
     variable_declared_in_function2 = 2

def a():
     global variable_declared_in_function1
     global variable_declared_in_function2
     print variable_declared_at_line_level1
     print variable_declared_at_line_level2

def b():
     global variable_declared_in_function1
     global variable_declared_in_function2
     print variable_declared_at_line_level1
     print variable_declared_at_line_level2

init_game_variables()
a()
b()
>>> 1
>>> 2
>>> 1
>>> 2

# It's such a simple concept. I don't know why python doesn't have a
"declare-global-for-all-functions." I hate to have to type
"game.player" or "game.badguy" for every other thing too. It doesn't
make any sense. A variable should be able to be accessed with minimal
effort. Because that's what the whole program is about. Accessing
variables and displaying them.

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.

And the more straight-forward it is, the less harder you make it for yourself.

Chart out the variables. Don't group any unnecessarily.
Chart out the functions. Don't put any functions inside of others unnecessarily.
Simplify everything. Don't give unnecessary fancy names to anything
that shouldn't even be there in the first place.
Give everything sensible and technically accurate names.

And your program should be so easy . . .
It saves hours of pointless conventional debugging and hair pulling frustration.
I guess Python still has many flaws.

On 3/10/12, Christopher Night <cosmologicon@xxxxxxxxx> wrote:
> It's not clear at all what you mean by "line-level". It sounds like you
> mean variables that are at the global scope, ie, you want to declare global
> variables. The answer is you use the "global" keyword within the function.
> Sorry if this seems like a lot of extra work, but I doubt you'll find much
> sympathy, since what you're doing is discouraged by most python users.
>
> One other possibility that I doubt you'll prefer is declaring a global
> object, and every time you want to declare a variable on the global scope,
> you set it to a member of that object. To wit:
>
> class globalstuff: pass
>
> def function1():
>     globalstuff.x = 100
>
> def function2():
>     print globalstuff.x
>
> Don't freak out at the fact that I used the "class" keyword. globalstuff is
> not what you normally think of as a class. That's just how you declare a
> namespace in python.
>
> If "line-level" means something other than global variables, please explain
> in more detail.
>
> -Christopher
>
> On Sat, Mar 10, 2012 at 3:39 PM, Brian Brown <brobab@xxxxxxxxx> wrote:
>
>> My plan is to make my program "function-oriented."
>> (Much less complicated than creating unnecessary classes and modules
>> (each with more functions) for something as simple as a small game
>> with just integer variables and string variables.)
>> Only using "line-level" and "function-level" with "global variables"
>> makes everything simple and easy. I don't understand why there's so
>> much hype about creating a new "class" or "module" whenever possible--
>> as if it will somehow magically make a program execute with more
>> satisfactory results.
>>
>> Thank you Ciro, but yes, I think you didn't answer my question.
>>
>>
>> On 3/10/12, Ciro Duran <ciro.duran@xxxxxxxxx> wrote:
>> > You can just declare your variables inside a function and their scope
>> will
>> > only reach inside that function.
>> >
>> > If you declare module variables (or global variables, if you fancy that
>> > name more) you can refer them inside functions without adding anything.
>> But
>> > if you want to assig something to the variable (eg. Create an object)
>> > you
>> > must specify the global keyword at the beginning of the function.
>> >
>> > Sorry if I didn't get the point of your question.
>> >
>> > Ciro
>> >
>> > El sábado 10 de marzo de 2012, Brian Brown <brobab@xxxxxxxxx> escribió:
>> >> 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.
>> >> Matt
>> >>
>> >
>>
>