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?

asked Nov 25, 2009 at 17:36

1 Answer 1

4

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)
answered Nov 25, 2009 at 17:46
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.