calling class methods without instance

J

Josh Close

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.

-Josh
 
B

Bengt Richter

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:
... def printSomething(message):
... print message
... printSomething = staticmethod(printSomething)
... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,678
Latest member
Aniruddha Das

Latest Threads

Top