| | |
What is a static method?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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 |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






