Classes as namespaces?

Gregory Ewing greg.ewing at canterbury.ac.nz
Sat Mar 27 18:56:21 EDT 2010


Jonathan Hartley wrote:

> def account_for_non_square_pixels(x):
>    ((some complex logic))
> account_for_non_square_pixels()

> class account_for_non_square_pixels:
>   ((some complex logic))

I don't see much advantage -- you're still leaving behind an
object that won't be used again.

If you're concerned about namespace pollution, there are a
couple of ways to clean it up:

1) Delete the function after using it:

   def account_for_non_square_pixels(x):
      ...
   account_for_non_square_pixels()
   del account_for_non_square_pixels

2) Use __all__ to specify which names you intend to export
(doesn't prevent anyone from importing something explicitly,
but at least it makes your intention clear, stops irrelevant
things appearing in dir() or help(), etc).

-- 
Greg



More information about the Python-list mailing list