I'm having some troubles with the scope in Python.
I initialize a variable (basePrice
) in a class (STATIC
). The value is static and the initialization takes some work, so I only want to do it once. Another class Item
, is a class that is created a lot. An Item
object needs to calculate its variable price
by using basePrice
. How can I initialize basePrice
only once and use it in an Item
object?
class STATIC:
basePrice = 0
def __init__(self):
self.basePrice = self.difficultCalculation()
def getBasePrice()
return self.basePrice
import STATIC
class Item:
price = 0
def __init__(self,price_):
self.price = price_ - STATIC.getBasePrice()