Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid using global without confusing new programming students in Python?

I've been teaching 8th-9th graders basic computer programming for two weeks, and yesterday I tried to show them how they could make real simple text-adventure games in Python.

Scenes are functions, (e.g dragons_cave()) which consist of some print statements and then a call to input(), asking the player where they want to go next, which then gets passed to globals() to find the appropriate function and then called. I know it's not ideal (at what point would the huge chain of functions start becoming a problem?) but, of what crossed my mind, it seems to be the simplest for them while involving only a little handwaving.

My problem is with global state — ex. the player gets a key in one scene and only then can they unlock the gate in another scene. When I have global immutables like strings or booleans, Python wants me to use the global keyword at the beginning of the function.

global hasKey
hasKey = True

I'm pretty okay with that, but I have a vague sense (picked up from Stackoverflow among other places on the Internet) that global is frowned upon and always has a superior counterpart. I could have a global dictionary or wrap everything in a class, but I'm not sure if I could defend those options clearly to my kids (who are still thinking through the implications of variables).

Whatever I use, I want to be able to explain straightforwardly to my kids why we do it this way and why doing it this way is necessary. global seems to have both these properties, but is it bad?

like image 726
Eli Rose Avatar asked Jul 20 '13 18:07

Eli Rose


2 Answers

I would encourage them to start learning OO

class Location:
     name="a place"
     description = "A dark place.  there are exits to the North and East"
     exits = "North","East"
     def __str__(self):
         return "%s\n%s"%(self.name,self.description)


class Player:
     current_location = "Home"
     inventory = ["Blue Key","Magic Thumbtacks"]
     health = 100
     name = "Unknown"
     def __init__(self,name):
         self.name = name

player = Player("Player 1")
loc = Location()
print loc
x = input("Input:")

To be honest a game is a difficult concept (even a text adventure). But I would start them directly on OO concepts, they will benefit more from that in the long term.

Granted this example is very small and leaves a lot of implementation details out.

An unrelated but better OO example would be:

class Animal:
     voice = '...'
     def speak(self):
         return "A %s Says '%s'"%(self.__class__.__name__, self.voice)

class Dog(Animal):
     voice = "Bark, Bark"

class Duck(Animal):
     voice = "Quack, Quack"

print Dog().speak()
print Duck().speak()
like image 78
Joran Beasley Avatar answered Oct 13 '22 22:10

Joran Beasley


Assuming you want to keep things as simple as possible (no OO) and want to avoid introducing the global keyword, you could instead use a state dictionary and assign variables in there.

state['hasKey'] = True

Since access to this dict is not a variable assignment you avoid introducing the global keyword and at the same time can teach how to use a dict (checking for keys etc.)

Of course you still use a global variable and don't really address the issue of good coding style. But on the other hand, it could serve as an introduction to scopes and namespaces.

like image 28
Michael Mauderer Avatar answered Oct 13 '22 22:10

Michael Mauderer