[Python-ideas] Idea: Named code blocks / inline module declarations

Ben Finney ben+python at benfinney.id.au
Wed Sep 17 10:39:08 CEST 2014


David Mertz <mertz at gnosis.cx> writes:

> Why is this a misuse? Classes are largely just namespaces to start
> with, and if you want to use them solely for that purpose, it's all
> there for you.

I think it's a misuse of the semantic meaning of classes to use them as
pure namespaces. The semantic intent conveyed by defining a class is
that you're defining a class *of objects*, and therefore that the class
is intended to be instantiated.

The programmer reading a class definition is receiving a strong signal
that there will be objects of this class in the program. To have a class
and not instantiate it, merely to have a namespace, is at least
misleading the reader of that code.

To do this is not an error. But it is IMO a code smell. If you're
defining a class and using it only as a namespace, your design is likely
poor and you need to re-think it.

In this case, I think Tennessee's intent is much better met using
modules; those *are* semantically namespace singletons, matching the
intent here and therefore much better at communicating that intent. I
see no justification given here for avoiding modules if this is what's
needed.

-- 
 \     Lucifer: “Just sign the Contract, sir, and the Piano is yours.” |
  `\     Ray: “Sheesh! This is long! Mind if I sign it now and read it |
_o__)                                later?” —http://www.achewood.com/ |
Ben Finney



More information about the Python-ideas mailing list