I have Python class with a number of functions. Evaluating each function requires a long list of constants (a few dozen); the constants are different for each function. Each function will be called many times (i.e. millions), so performance is a major concern. What is the proper way to handle this situation, both in terms of readability and in terms of speed? (I'm relatively new to python)
I could make the constants class attributes, but this seems to clash with the safety provided by scopes (each constant is used only locally in a single function). It would be convenient to be able to use the same variable names in different functions. Besides, I find that all the self.
s make the code unreadable, especially when trying to keep line length under 79.
I now have the constants defined locally at the start of each function. This is reasonably clear, but I'm not sure if it's optimal in terms of performance? Is new memory allocated and freed for every function call? Especially since you apparently can't declare proper constants in Python.
Maybe it's better to put the parameters for each function in a separate class and pass an object of this class as an argument? This would maintain the proper scopes while ensuring each constant is defined only once?
Aucun commentaire:
Enregistrer un commentaire