Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping constants in python

This is mainly a "good python style" question.

I have a module which is using a number of constants that feels should be grouped.

Lets say we have Dogs and cat and each of them have number of legs and favorite food.

Note that

  1. we want to model nothing but those constants about Dogs and Cats
  2. quite probably we'll have more animals in the future.
  3. those constants wont be used outside of the current module.

I thought about the following solutions:


Constants at module level

DOG_NUMBER_OF_LEGS = 4
DOG_FAVOURITE_FOOD = ["Socks", "Meat"]
CAT_NUMBER_OF_LEGS = 4
CAT_FAVOURITE_FOOD = ["Lasagna", "Fish"]

They seem not really grouped, but I think it is the solution I prefer.


Classes as namespaces

class Dog(object):
  NUMBER_OF_LEGS = 4
  DOG_FAVOURITE_FOOD = ["Socks", "Meat"]
class Cat(object):
  NUMBER_OF_LEGS = 4
  FAVOURITE_FOOD = ["Lasagna", "Fish"]

I don't like this solution as we'll have class that we wont use and they can be actually instantiated.


Dictionary of constants

ANIMALS_CONFIG = { 
   "DOG" : { 
     "NUMBER_OF_LEGS" : 4,
     "FAVOURITE_FOOD" : ["Socks", "Meat"]
   },
   "CAT" : { 
     "NUMBER_OF_LEGS" : 4,
     "FAVOURITE_FOOD" : ["Lasagna", "Fish"]
   }
}

I also thought about adding submodules but I dont really want to expose those internal constants


what is the most pythonic way to do it / how would you do it?

like image 768
Mario Corchero Avatar asked Apr 22 '15 08:04

Mario Corchero


People also ask

How do you store constants in Python?

Storing Constants in Configuration Files You may need to keep all your constants out of your project's source code. To do this, you can use an external configuration file. This file uses the INI file format. You can read this type of file using the configparser module from the standard library.

Can you declare constants 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.

What are the constants in Python?

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.

How many types of constant are there in Python?

Python uses four types of constants: integer, floating point, string, and boolean.


1 Answers

I would go for a fourth option, preferring a collections.namedtuple:

Animal = namedtuple('Animal', 'number_of_legs favourite_food')

You then create instances like:

DOG = Animal(4, ['Socks', 'Meat'])
CAT = Animal(4, ['Lasagna', 'Fish'])

and access the values externally as:

from animals import CAT

print CAT.number_of_legs

There's really no point having classes if you don't need to create any methods, and I think the form of access above is neater than e.g.:

from animals import animals

print animals['CAT']['number_of_legs']

namedtuples, like vanilla tuples, are immutable, too, so you can't accidentally reassign e.g. CAT.number_of_legs = 2somewhere.

Finally, the namedtuple is a lightweight data structure, which may be important if you're creating lots of animals:

>>> import sys
>>> sys.getsizeof({'number_of_legs': 4, 'favourite_food': ['Lasagna', 'Fish']})
140
>>> from collections import namedtuple
>>> Animal = namedtuple('Animal', 'number_of_legs favourite_food')
>>> sys.getsizeof(Animal(4, ['Lasagna', 'Fish']))
36
like image 155
jonrsharpe Avatar answered Oct 02 '22 06:10

jonrsharpe