views:

337

answers:

3

I want to do something like the following

class A:
  def static_method_A():
    print "hello"

def main(param=A):
  param.static_method_A()

I want this to be equivalent to A.static_method(). Is this possible?

+3  A: 

Sure. Classes are first-class objects in Python.

Although, in your example, you should use the @classmethod (class object as initial argument) or @staticmethod (no initial argument) decorator for your method.

Chris Jester-Young
Way to change to my answer.
Unknown
Nope, my answer isn't based on yours. However, I _am_ guilty of writing a one-sentence answer just to get in a first shot, then expanding it within the 5-minute grace.
Chris Jester-Young
This is the same Unknown that was giving me the same garbage the other day... I took my time to write out a thoughtful answer and help someone out and wasn't even paying attention to what anyone else was posting. I gave him a pity +1, but now I regret it. I think everyone needs to stop being so reputation-hungry and just try to help each other out.
Tom
Thanks, Tom, you win; I agree with you totally. (I just replaced my last comment to Unknown with something more sarcastic, just because his picking on other poor users just made me irate.)
Chris Jester-Young
Why do you think I am picking on other users? I am annoyed that people like you and Tom either post nothing or the wrong answer, and then 30 seconds later, I see you and 2 or 3 other people writing the exact same thing as I did.
Unknown
@Unknown: You don't get it, do you? To get "first post" often (some) people post a simple one-liner answer and edit it afterwards, exploiting the 5-minute grace to do so. If you didn't answer at all (e.g., if you took a holiday from SO, which I guarantee will reduce your blood pressure :-)), the same answer would have resulted. Trust me on that one.
Chris Jester-Young
To think that you're the only person who could have come up with the answer, and that everyone must be copying you, that's the sense of self-importance I'm talking about. Stop thinking you're Einstein, and your life will go a lot easier, seriously. To be honest, I wish Jeff would implement my favourite feature; once implemented, you can never accuse anyone of copying your answers anymore: http://stackoverflow.uservoice.com/pages/general/suggestions/69272-implement-a-twit-filter-
Chris Jester-Young
This is called The Fastest Gun In The West issue. If you are fast enough to get in a simple answer, you will still have a five minute grace period to elaborate on your answer and still maintain your top spot in the order. I've done this numerous times to take a short answer and pile on loads more information and resources. It doesn't mean that anybody is copying anybody else.
TheTXI
As long as the fast simple answer has useful information in it, I don't mind that it is not completely thorough. In my opinion, whatever answers the question is sufficient and a speedy response is very useful for the person asking the question. Going back and changing the answer to be more thorough is useful for future readers who have a similar problem.
Jesse Shieh
A: 

Sure why not? Don't forget to add @staticmethod to static methods.

class A:
  @staticmethod
  def static_method_A():
    print "hello"

def main(param=A):
  param.static_method_A()
Unknown
Link to python docs (http://docs.python.org/library/functions.html#staticmethod) on static method
Tom Leys
Unknown, don't claim credit for musicfreak's answer or mine. If my post was the only one you commented on that way, I'd let it go; I'm sure enough of my answer to know I copied off nobody. But musicfreak is a newbie to SO; you've got to be better to other users than that. Falsely accusing them of copying you is just...thinking too highly of yourself. :-P
Chris Jester-Young
@Chris its just way too suspicious that you and musicfreak had content-less answers that basically said "yes". Then when I had my staticmethod answer, 1 minute later, both of you changed to have the same answer as I.
Unknown
Not suspicious at all - that is the normal way of things on Stack Overflow, when you have a question where a single answer is as obvious as it is here.
David Zaslavsky
@David if it was obvious then why were there 3 different answers until I came up with mine? And then they all switched?
Unknown
+2  A: 

You should be able to do the following (note the @staticmethod decorator):

class A:
  @staticmethod
  def static_method_A():
    print "hello"
def main(param=A):
  param.static_method_A()
Greg Hewgill