Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a Python module, what are the standard global variables to declare?

Tags:

python

In reading other Python modules, I've seen that many people often include __version__ and __author__ global variables in their source files (its even mentioned in PEP3001). I'd like to document my code with a reasonable set of these variables. What is a list of global variables that might be commonly included?

like image 333
Willi Ballenthin Avatar asked Jan 21 '13 17:01

Willi Ballenthin


People also ask

How to declare a global variable in Python?

How to declare a global variable in Python? What is a global variable? A global variable is a variable that is declared outside the function but we need to use it inside the function. Here, variable a is global. As it is declared outside the function and can be used inside the function as well. Hence the scope of variable a is global.

What happens if you don’t use global in Python?

If you don’t use global within a function, Python uses the local namespace and the variable is local. It goes away after the function completes. So to modify a global variable in a function’s block, you must use a global statement to declare that the variable is defined in the global scope:

How do you define a global variable between modules?

The global keyword defines a variable as global in the context of the module it is in, not between modules. Also, from the official Python FAQ: How do I share global variables across modules? Show activity on this post.

What is global scope of a variable in Python?

Identifiers defined outside any function (or class) have global scope, these may include functions, variables and classes. Variables with global scope are known as global variables. Identifiers with global scope can be used in a .py file or interactive session anywhere after they’re defined.


2 Answers

There isn't a specific standard for those global variables - as noted in the PEP you linked, they were attempts to achieve a standard but haven't become universally accepted in any singular form.

The real standard is the PyPI metadata, which is specified in the setup.py file for your module, using distutils (or a compatible interface). Here's the example from the packaging tutorial:

from distutils.core import setup

setup(
    name='TowelStuff',
    version='0.1.0',
    author='J. Random Hacker',
    author_email='[email protected]',
    packages=['towelstuff', 'towelstuff.test'],
    scripts=['bin/stowe-towels.py','bin/wash-towels.py'],
    url='http://pypi.python.org/pypi/TowelStuff/',
    license='LICENSE.txt',
    description='Useful towel-related stuff.',
    long_description=open('README.txt').read(),
    install_requires=[
        "Django >= 1.1.1",
        "caldav == 0.1.4",
    ],
)

http://guide.python-distribute.org/creation.html

like image 140
Amber Avatar answered Oct 06 '22 00:10

Amber


Use distutils (or the superset setuptools) instead to provide metadata about your project.

Especially when using setuptools, that metadata is then discoverable and reusable through the pkg_resources module.

There is no standard for global variables such as __version__, not even for the Python stdlib, which is why the effort to provide this metadata in the stdlib for Python 3 has not amounted to anything.

I can recommend the Python Packaging User Guide as a primer on how to package your project properly.

like image 32
Martijn Pieters Avatar answered Oct 05 '22 23:10

Martijn Pieters