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?
asked Nov 25, 2009 at 17:36
Wells
11k16 gold badges58 silver badges94 bronze badges
1 Answer 1
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)
Sign up to request clarification or add additional context in comments.
Comments
lang-py