What is a static method?

Thread Solved

Join Date: Feb 2009
Posts: 148
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster

What is a static method?

 
0
  #1
Oct 19th, 2009
What is a static method and when should I use one?
"The man who smiles when something goes wrong has through of someone else to blame"; One of my friends said this on facebook.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
2
  #2
Oct 19th, 2009
  1. # staticmethod() and classmethod()
  2. # make it easier to call one class method
  3.  
  4. class ClassA(object):
  5. def test(this):
  6. print this
  7. test = staticmethod(test)
  8.  
  9. ClassA.test(4) # 4
  10.  
  11. class ClassB(object):
  12. @classmethod
  13. def test(self, this):
  14. print this
  15.  
  16. ClassB.test(4) # 4

See:
http://www.techexperiment.com/2008/0...ods-in-python/
Last edited by bumsfeld; Oct 19th, 2009 at 1:54 am.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 148
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster
 
0
  #3
Oct 20th, 2009
That wasn't very clear to me... I thing a worded explanation would be much more useful
Last edited by mahela007; Oct 20th, 2009 at 7:13 am.
"The man who smiles when something goes wrong has through of someone else to blame"; One of my friends said this on facebook.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 905
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is online now Online
previously paulthom12345
 
0
  #4
Oct 20th, 2009
A static method is one that can be called without instantiating the class
so
  1. >>> class T(object):
  2. def test():
  3. print "HI"
  4.  
  5.  
  6. >>> T.test()
  7.  
  8. Traceback (most recent call last):
  9. File "<pyshell#4>", line 1, in <module>
  10. T.test()
  11. TypeError: unbound method test() must be called with T instance as first argument (got nothing instead)
  12. >>>

See how that doesnt work? Well we can make it work by making it into a static method

  1. >>> class T(object):
  2. @staticmethod
  3. def test():
  4. print "HI"
  5.  
  6.  
  7. >>> T.test()
  8. HI
  9. >>>

So, hopefully that shows what it does
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 920
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #5
Oct 20th, 2009
A possible use of static methods is to add constructors to a class, like in this example
  1. class Person(object):
  2.  
  3. def __init__(self, name, age):
  4. self.name = name
  5. self.age = age
  6.  
  7. def __str__(self):
  8. return "Person({name}, {age})".format(name=self.name, age=self.age)
  9.  
  10. @staticmethod
  11. def from_sequence(seq):
  12. name, age = list(seq)
  13. return Person(name, age)
  14.  
  15. @staticmethod
  16. def from_dict(dic):
  17. return Person(dic["name"], dic["age"])
  18.  
  19. if __name__ == "__main__":
  20. my_tuple = ("John", 32)
  21. my_dict = {"name":"Fred", "age":55}
  22.  
  23. anna = Person("anna", 15)
  24. john = Person.from_sequence(my_tuple)
  25. fred = Person.from_dict(my_dict)
  26.  
  27. for person in (anna, john, fred):
  28. print(person)
Last edited by Gribouillis; Oct 20th, 2009 at 7:54 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 148
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster
 
0
  #6
Oct 21st, 2009
Thanks a lot Paul T and Gribouillis.. I think this is the ideal type of answer for a forum like this.
"The man who smiles when something goes wrong has through of someone else to blame"; One of my friends said this on facebook.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 148
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster
 
0
  #7
Oct 21st, 2009
Thanks a lot Paul T and Gribouillis.. I think this is the ideal type of answer for a forum like this.
"The man who smiles when something goes wrong has through of someone else to blame"; One of my friends said this on facebook.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC