Constants in python?

python

Solution

If they're all within a single module, then they only live in that module's namespace and you don't have to worry about name clashes. (And you can still import them into other namesapaces)

For example

MyModWithContstants.py

x = 0
y = 0

def someFunc():
  dosomethingwithconstants(x,y)

and we can also do

anotherMod.py

from MyModWithConstants import x
# and also we can do
import MyModWithConstants as MMWC

def somOtherFunc():
  dosomethingNew(x, MMWC.y)  
  ## x and MMWC.y both refer to things in the other file

Problem

I have the following variables declared in a lot of functions, as I need those values in each one of them. Is there anyway I can declare them at a global scope or something, such as I won't have to declare them in all my methods? I am using all this methods on instance methods of a class of mine. ``` x = 0 y = 1 t = 2 ``` In c# I'd just declare them as global class variables, but the problem is that I don't want to have to use them always as self.x, self.y and self.z, as it gets my algorithm's code uglier than it already is. How would you do this? A typical usage of this would be: ``` def _GetStateFromAction(self, state, action): x = 0 y = 1 t = 2 if (action == 0): return (state[x], state[y] - 1, state[t]) if (action == 1): return (state[x] - 1, state[y], state[t]) ```

Original source