__magic__ Variable Conventions


Posted

in


Via PythonWare’s Daily Python-URL, Alan Green covers module level __magic__ variables in Python.  I’ll have to admit that I knew about some of them and was clueless about others.  For example, __dict__ is a module global read-only dictionary.  You can also explicitly set the public names of a module with __all__.

I’m guilty of using several __magic__ veriables that aren’t explicitly defined in the language reference.  Most came from looking at the source code of third party Python modules, particularly those by Mark Pilgrim.  If it’s good enough for Mark, it should work for me, right?  Here’s a list of __magic__ variables that you’ll commonly find in modules written by myself and others:

  • __license__: String.  Useful for identifying the license of the code.  Examples: “GPL”, “LGPL”, “BSD”, etc.
  • __history__: String.  Excellent for keeping track of changes between versions.  Similar to a changelog.txt file.  See Mark’s feedparser for an example.  This works for me, but could get out of hand for larger modules with multiple authors.
  • __copyright__: String.  Copyright info goes here.  I’ve also seen llicense info here (for example xmltramp by Aaron Swartz).

Are there other useful but unofficial __magic__ variables that are useful?  Email me and I’ll append them to the list.  Would it be worthwhile to compile a list of commonly used but unofficial variables into a PEP?  It would be excellent if pydoc took advantage of this extra information.