tags:

views:

109

answers:

1

I have:

lib/
lib/__init__.py
lib/game.py

In __init__.py I'd like to define a variable that can be accessed by any class inside lib, like so:

BASE = 'http://www.whatever.com'

And then inside game.py, acces it inside in the Game class:

class Game:

def __init__(self, game_id):
    self.game_id = game_id

    url = '%syear_%s/month_%s/day_%s/%s/' % (lib.BASE, year, month, day, game_id)

Yeah so clearly 'lib.BASE' isn't right- what's the convention here? Is there a tidier, more pythonic way to handle what I'd call package-global variables?

+4  A: 

See http://docs.python.org/tutorial/modules.html#intra-package-references

So you could have a lib/settings.py file which contains the line

BASE = 'http://www.whatever.com'

and then say

from settings import *

in game.py you should then be able to write

url = '%syear_%s/month_%s/day_%s/%s/' % (BASE, year, month, day, game_id)
regjo