calling class methods without instance

Bengt Richter bokr at oz.net
Wed Sep 22 17:19:17 EDT 2004


On Wed, 22 Sep 2004 15:38:56 -0500, Josh Close <narshe at gmail.com> 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.

Just copied your code above and pasted into a python console session:

 >>> class myClass:
 ...   def printSomething(message):
 ...     print message
 ...   printSomething = staticmethod(printSomething)
 ...
 >>> myClass.printSomething('This did not work for you?')
 This did not work for you?

Suggest you try the same and copy/paste from your screen to a post here
to show the error (or "didn't seem to work" symptom) you got.

(windows nt4, python 2.3 here, what's your system?)

Regards,
Bengt Richter



More information about the Python-list mailing list