Python Static methods, why?

Possible Duplicate:
What is the difference between @staticmethod and @classmethod in Python?

I have a few questions about staticmethods in classes. I will start by giving an example.

Example one:

class Static:
    def __init__(self, first, last):
        self.first = first
        self.last = last
        self.age = randint(0, 50)
    def printName(self):
        return self.first + self.last
    @staticmethod
    def printInfo():
        return "Hello %s, your age is %s" % (self.first + self.last, self.age)

x = Static("Ephexeve", "M").printInfo()

Outputs:

Traceback (most recent call last):
  File "/home/ephexeve/Workspace/Tests/classestest.py", line 90, in <module>
    x = Static("Ephexeve", "M").printInfo()
  File "/home/ephexeve/Workspace/Tests/classestest.py", line 88, in printInfo
    return "Hello %s, your age is %s" % (self.first + self.last, self.age)
NameError: global name 'self' is not defined

Example two:

class Static:
    def __init__(self, first, last):
        self.first = first
        self.last = last
        self.age = randint(0, 50)
    def printName(self):
        return self.first + self.last
    @staticmethod
    def printInfo(first, last, age = randint(0, 50)):
        print "Hello %s, your age is %s" % (first + last, age)
        return

x = Static("Ephexeve", "M")
x.printInfo("Ephexeve", " M") # Looks the same, but the function is different.

Outputs

Hello Ephexeve M, your age is 18

I see I cannot call any self.attribute in a staticmethods, I am just not really sure when and why to use it. In my mind, if you create a class with a few attributed, perhaps you want to use them later on, and not have a staticmethod where all attributes are not callable. Anyone can explain me this? Python is my first programming langunge, so if this is the same in Java for example, I don't know.


What are you trying to achieve with that staticmethod ? How are you expecting it to solve whatever your problem is if you don't know what it does?

Or are you just playing around to see what staticmethod does? In that case, it would probably be more productive to read the docs to be told what it does, rather than randomly applying it and trying to guess from the behaviour what it does.

In any case, applying @staticmethod to a function definition in a class makes a "static method". "Static" is unfortunately one of the most confusingly overloaded terms in programming; here it means that the method does not depend on or alter the object's state. If I have a static method foo defined in a class Bar , then calling bar.foo(...) (where bar is some instance of the class Bar ) will do exactly the same thing regardless of what bar 's attributes contain. Indeed I can call it directly from the class as Bar.foo(...) when I don't even have an instance!

This is achieved by simply not passing the instance into the static method, thus static methods have no self parameter.

Static methods are rarely necessary, but occasionally convenient. They are really the same as simple functions defined outside the class, but putting them in the class marks them as "associated" with the class. You'd generally use them for calculating or doing things that are closely related to the class, but aren't actually operations on some particular object.

链接地址: http://www.djcxy.com/p/9170.html

上一篇: Python:静态方法和类方法的区别

下一篇: Python静态方法,为什么?