Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python style: lowercase class names for "namespaces"?

I know the PEP-8 convention for class names is ClassName. But we often use small classes as pseudo-namespaces, enums etc. In other words, not a real class that you're going to instantiate. We've opted for a lowercase naming convention for such "classes", because they're really a namespace/enum name.

Does anyone else have in-house styles for this, or other ways of achieving the same thing?

An example:

import urllib2

class config:  # pseudo-namespace for module-level config variables
    api_url = 'http://example.com/api'
    timeout = 1.5
    debug = True

class countries:  # pseudo-enum
    new_zealand = 1
    united_states = 2

def func():
    if config.debug:
        print 'Calling func()'
    return urllib2.urlopen(config.api_url)
like image 940
Ben Hoyt Avatar asked Dec 07 '10 18:12

Ben Hoyt


People also ask

Should Python class names be capitalized?

In proper python code, your class name should be capitalized. This helps to differentiate a class from a normal variable or function name. Understanding the difference between class attributes and instance attributes is really important because it helps you understand how your objects are made.

Should Python modules be lowercase?

Package and Module Names Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

What are the Python naming conventions?

A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)


4 Answers

For all enums and constants, I prefer to use capitalized versions.

class COUNTRIES:  # pseudo-enum
    NEW_ZEALAND = 1
    UNITED_STATES = 2

It is still ok with me, if the class name is not all capitalized. Since, any way it is tied up with the enum values. I am always going to use it like Countries.NEW_ZEALAND, which tells me that it is an enum.

class Countries:  # pseudo-enum
    NEW_ZEALAND = 1
    UNITED_STATES = 2
like image 157
pyfunc Avatar answered Oct 02 '22 15:10

pyfunc


You can also create a module named config with the following content:

api_url = 'http://example.com/api'
timeout = 1.5
debug = True
like image 27
Cristian Ciupitu Avatar answered Oct 02 '22 16:10

Cristian Ciupitu


Why not

class PseudoNamespace: pass

config = PseudoNamespace()
config.api_url = 'http://example.com/api'
config.timeout = 1.5
config.debug = True

countries = PseudoNamespace()
config.new_zealand = 1
config.united_states = 2

if you really care about the PEP?

like image 30
agf Avatar answered Oct 02 '22 14:10

agf


I use dictionaries instead:

config = dict(
    api_url = 'http://example.com/api',
    timeout = 1.5,
    debug = True)

countries = dict(
    new_zealand = 1,
    united_states = 2)

If you find attribute access cumbersome in a Python dict, try an attrdict:

class attrdict(dict):
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)
        self.__dict__ = self

It allows to access dictionary entries with keys that are valid identifiers as attributes, e.g. config.api_url instead of config["api_url"].

Of course I use lower-case names for these.

like image 26
Sven Marnach Avatar answered Oct 02 '22 14:10

Sven Marnach