Reputation: 397
I am trying to define some constants at the top of my file. There are no classes in the file, just imports, constants, and methods. Possibly due to poor design, I want to use a method inside of this file to set a constant. For example:
MY_CONSTANT = function(foo, bar)
def function(foo, bar):
return 6
In this example, I want MY_CONSTANT to get assigned the int 6. This is a simplified version of the problem, as my function actually makes many expensive calls and I only want that function to be called once. I plan to use the constant inside of a loop.
This does not work because I get the following error:
NameError: name 'function' is not defined
Is there a better design for this, or how can I use a method call to set my constant?
Upvotes: 0
Views: 2177
Reputation: 5289
You are trying to call a function before it has been defined:
def function(foo, bar):
return 6
MY_CONSTANT = function(foo=None, bar=None)
>>>MY_CONSTANT
6
Edit: I set foo=None
and bar=None
going into function
because I'm not sure where you have those defined either.
Upvotes: 2