Unification of Methods and Functions

Duncan Booth me at privacy.net
Fri May 28 04:10:46 EDT 2004


David MacQuigg <dmq at gain.com> wrote in 
news:eu9cb0tkf674ob8u595sqk9kncs8nvm8k9 at 4ax.com:

> To summarize our discussion on static methods, we have four ways to
> accomplish the purpose of making a method callable without an
> instance.
> 
> 1) Add the staticmethod wrapper.
> 2) Create a dummy instance, and use it to call the method.
> 3) Move the method outside of the class, and if that disturbs the
> modular structure of the program, make the class and its external
> function a new module with its own namespace.
> 4) Move the method outside of the class, but make sure it is still
> "associated" with the class by giving it a special name.
> 
> Is there any reason we *should not* use solution #1?

You missed 5) Use a classmethod wrapper.

The commonest reason I know of for wanting to a method callable without an 
instance is to provide the class with factory functions, and in that case 
you definitely don't want to use solution #1 as solution #5 gives you 
support for subclassing at no extra charge.




More information about the Python-list mailing list