Some function need 'constant' values (ie. not designed to be redefined later) that are not to be parametrized. While default arguments are stored only once for each function, some are just not very meaningful to be put as parameters (ie. to be part of the signature). For (a not very useful) example:
def foo(bar):
my_map = {"rab": barType, "oof": fooType}
return my_map.get(bar,defaultType)()
It wasted CPU time and RAM space to re-define such a constant for each call. Some other ways are to store such constants as module level globals or make the function a callable class, but there may be other ways, maybe?
When doing the module level global way, I prefix my (meant as a) constant variable with a "_" to show that it is there not for anyone's interest. Still I feel the module namespace slightly "polluted", not to speak of the shame of using something as discouraged as globals at all:
_my_map = {"rab": barType, "oof": fooType}
def foo(bar):
return _my_map.get(bar,defaultType)()
Or the transform it into a class way. I make the __call__
a classmethod, to avoid the need of creating instances:
class foo:
my_map = {"rab": barType, "oof": fooType}
@classmethod
def __call__(cls,bar):
return cls.my_map.get(bar,defaultType)()
Are these solutions pythonic enough?
Are there other ways to do this?
Is it even ok as a practice to use such 'constants'?
Note these objects in my examples are not necessarily actual constants, but used (and could be thought) as such by their purpose.
Assigning value to constant in Python In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.
To run a function only once:Set a has_run attribute on the function to True the first time it runs. Each time the function is called, check if the has_run attribute is True and return straight away. The code in the function will only run the first time.
A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants. A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.
Constants in Python A constant is a type of variable that holds values, which cannot be changed. In reality, we rarely use constants in Python. Constants are usually declared and assigned on a different module/file. Then, they are imported to the main file.
Set it as an attribute on the function:
def foo(bar):
return foo.my_map.get(bar, defaultType)()
foo.my_map = {"rab": barType, "oof": fooType}
A callable class or a closure is not simple enough IMO.
IMHO, there is nothing wrong with module level constants.
Note that according to PEP 8, constants should be all upper case, like this:
_MY_MAP = {"rab": barType, "oof": fooType}
def foo(bar):
return _MY_MAP.get(bar,defaultType)()
The regular expression module in the standard library uses this style and many established third-party libraries do as well. If you are not convinced, just go to your site-packages
directory and grep:
egrep "^_?[A-Z]+ =" *
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With