| | |
What is a static method?
Thread Solved |
2
#2 Oct 19th, 2009
python Syntax (Toggle Plain Text)
# staticmethod() and classmethod() # make it easier to call one class method class ClassA(object): def test(this): print this test = staticmethod(test) ClassA.test(4) # 4 class ClassB(object): @classmethod def test(self, this): print this 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!
0
#4 Oct 20th, 2009
A static method is one that can be called without instantiating the class
so
See how that doesnt work? Well we can make it work by making it into a static method
So, hopefully that shows what it does
so
Python Syntax (Toggle Plain Text)
>>> class T(object): def test(): print "HI" >>> T.test() Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> T.test() TypeError: unbound method test() must be called with T instance as first argument (got nothing instead) >>>
See how that doesnt work? Well we can make it work by making it into a static method
Python Syntax (Toggle Plain Text)
>>> class T(object): @staticmethod def test(): print "HI" >>> T.test() HI >>>
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
Check out my Site | and join us on IRC | Python Specific IRC
0
#5 Oct 20th, 2009
A possible use of static methods is to add constructors to a class, like in this example
python Syntax (Toggle Plain Text)
class Person(object): def __init__(self, name, age): self.name = name self.age = age def __str__(self): return "Person({name}, {age})".format(name=self.name, age=self.age) @staticmethod def from_sequence(seq): name, age = list(seq) return Person(name, age) @staticmethod def from_dict(dic): return Person(dic["name"], dic["age"]) if __name__ == "__main__": my_tuple = ("John", 32) my_dict = {"name":"Fred", "age":55} anna = Person("anna", 15) john = Person.from_sequence(my_tuple) fred = Person.from_dict(my_dict) for person in (anna, john, fred): print(person)
Last edited by Gribouillis; Oct 20th, 2009 at 7:54 am.
![]() |
Similar Threads
- non-static method cannot be referenced from a static context (Java)
- Static method doesn't actually return anything (Java)
- OOP Fatal error: Non-static method (PHP)
- Using a static method to read data from text file (Java)
- Trouble with a static method (Java)
- Non-Static method cannot be referanced from a Static context (Java)
- Non-static method can't be referenced from a static context (Java)
Other Threads in the Python Forum
- Previous Thread: Python While Loop Issues, Looking For Help
- Next Thread: Using google appengine with facebook?
| Thread Tools | Search this Thread |
address alarm anydbm app beginner cipher conversion coordinates curves cx-freeze data development dictionary directory dynamic examples excel feet file float format function generator getvalue gui halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pyglet pymailer python queue random recursion recursive screensaverloopinactive script scrolledtext searchingfile shebang slicenotation split ssh string strings table terminal text thread threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wx.wizard wxpython xlwt






