n611x007
n611x007

Reputation: 9282

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

Some function need 'constant' values (i.e. 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 (i.e. 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.

Upvotes: 17

Views: 8189

Answers (4)

Daniel Hepper
Daniel Hepper

Reputation: 29977

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

Upvotes: 6

Pavel Anossov
Pavel Anossov

Reputation: 62948

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.

Upvotes: 18

isedev
isedev

Reputation: 19641

You could also use closures:

def make_foo():
    my_map = {"rab": barType, "oof": fooType}
    def foo(bar):
        return my_map.get(bar,defaultType)()
    return foo
foo = make_foo()

Upvotes: 3

martineau
martineau

Reputation: 123531

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.

Upvotes: 1

Related Questions