In python, how to store 'constants' for functions only once?

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.


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 PEP8, 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]+ =" *

To make something that is as self-contained as possible. You could create a function object (aka functor) class and give it a __call__() method or a classmethod (but probably not both):

class bazType(object): pass
class barType(object): pass
class fooType(object): pass

class Foo(object):
    _DEFAULT_TYPE = bazType
    _MY_MAP = {"rab": barType, "oof": fooType}

    def __call__(self, bar):
        return self._MY_MAP.get(bar, self._DEFAULT_TYPE)()

    @classmethod
    def foo(cls, bar):
        return cls._MY_MAP.get(bar, cls._DEFAULT_TYPE)()

# using classmethod
print Foo.foo("rab")

# using __call__ method
foo = Foo()
print foo("baz")

# alternative way to use classmethod
foo = Foo.foo
print foo("oof")

Yet another alternative would be to define a staticmethod , which I won't illustrate because it's so similar to the other two -- but you get the idea I hope.

链接地址: http://www.djcxy.com/p/54276.html

上一篇: 函数作为类属性成为绑定方法

下一篇: 在python中,如何只为函数存储“常量”?