Does Python have Class methods

Magnus Lie Hetland mlh at idi.ntnu.no
Wed May 9 14:42:37 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9d9fk70gmr at news1.newsguy.com...
> "Aahz Maruch" <aahz at panix.com> wrote in message
> news:9d946l$6m1$1 at panix2.panix.com...
> > In article <3af80f95.73151117 at News.CIS.DFN.DE>,  <costas at meezon.com>
> wrote:
> > >
> > >Does Python support the concept of class methods (as opposed to
> > >instance methods)? If not is there a suggested way of doing this?
> >
> > The standard answer is to put the class (e.g. "foo") into its own module
> > (e.g. "foo.py") and use module-level functions.
>
> Right, but that supports "static methods" a la C++ rather than "class
> methods" a la Smalltalk.  Another alternative for "static methods" is at
> http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52304, for
> example.  Thomas Heller has also put up a Cookbook recipe, at
> http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52311,
> which exemplifies how to support "real" (Smalltalk-ish) class methods.

This is all very fancy and nice, but what is wrong with the following?

  def double(self, x):
      return x*2

  class Doubler:
      double = double

  d = Doubler()
  d.double(3)
  # returns 6

I remember a discussion earlier on (some years ago) about class variables
where I made a similar remark and someone explained the error of my ways.
Since replying to Alex's posts usually makes me look foolish, I
eagerly await the reply <wink>

> Alex

--

  Magnus Lie Hetland         http://www.hetland.org

 "Reality is that which, when you stop believing in
  it, doesn't go away."           -- Philip K. Dick





More information about the Python-list mailing list