calling class methods without instance
Josh Close
narshe at gmail.com
Wed Sep 22 17:10:46 EDT 2004
On Wed, 22 Sep 2004 13:55:44 -0700, Robert Brewer <fumanchu at amor.org> wrote:
>
>
> Josh Close wrote:
> > Is there a way to call a class method without an instance of
> > that class?
> >
> > class myClass:
> > def printSomething(message):
> > print message
> >
> > myClass.print()
> >
> > That's basically what I want to do. Is there a way to do that without
> > having to do
> >
> > c = myClass()
> > c.print()
> >
> > I've tried using
> >
> > class myClass:
> > def printSomething(message):
> > print message
> > printSomething = staticmethod(printSomething)
> >
> > but that didn't seem to work either. I'm probably just missing
> > something really simple here.
>
> Not sure what you're missing, unless you're not posting a complete
> example. This works:
>
> >>> class myClass:
> ... def p(msg):
> ... print msg
> ... p = staticmethod(p)
> ...
> >>> myClass.p("hey")
> hey
>
Took a closer look and I was doing something stupid. Static method works fine.
Thanks.
-Josh
More information about the Python-list
mailing list