calling class methods without instance

Robert Brewer fumanchu at amor.org
Wed Sep 22 16:55:44 EDT 2004


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


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list