文章来自《Python cookbook》.
翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!
-- 60.7.17.34 [2004-10-22 03:20:44]
1. 描述
Implementing Static Methods
实现Static方法
Credit: Alex Martelli, Carel Fellinger
1.1. 问题 Problem
You want to call methods directly on a class without supplying an instance of the class as the first argument, or on any instance without having the instance implicitly become the first argument.
需要直接调用类方法,无需提供类实例(或者可以隐式转换为类实例的对象)作为方法第一个参数情况。
1.2. 解决 Solution
In Python 2.2 (on either classic or new-style classes), the new built-in staticmethod function wraps any callable into a static method, and we just bind the same name to the staticmethod object in class scope:
Python 2.2中(对于新旧代码同样适用),新的内置staticmethod函数封装了对于静态函数的调用,只需要在类作用域范围内帮定相应函数名称到对应的静态函数。
1 class Greeter:
2 def greet(name): print "Hello", name
3 ####################################
4 greet = staticmethod(greet)
5 ####################################
6
7 #In Python 2.1 and earlier, we can easily simulate the same construct:
8
9 #python 2.1及早期版本中,可以如下处理,达到相同效果:
10
11 class staticmethod:
12 def _ _init_ _(self, anycallable): self._ _call_ _ = anycallable
Now, with any release of Python, we can say:
这样,在任何python中,都可以使用如下代码:
>>> greeting = Greeter( ) >>> greeting.greet("Peter") Hello Peter >>> Greeter.greet("Paul") Hello Paul
You can get a static method as a class attribute or as the attribute of any instance of the class. It does not matter which, because when you call the static method, it calls the underlying callable anyway.
可以将静态方法作为类属性,也可以将它作为类实例的属性. 没有什么区别, 因为无论使用何种形式调用静态方法,调用的都是底层的callable.
1.3. 讨论 Discussion
In Python, when you want to make a function available for calling, you normally expose it as an attribute of a module, not of a class. An attribute of a class object that starts out as a Python function implicitly mutates into an unbound method (see Recipe 5.13 for a way to exploit this). Thus, if you want to make the function available as a class attribute, without mutation, you need to wrap the function into a callable of another type and bind that wrapper callable as the class attribute. Python 2.2 offers a new built-in staticmethod type that performs just such a wrapping. This recipe shows how to use it and how to emulate it easily in earlier Python versions with a tiny auxiliary class of the same name.
python中,编写一个可以调用的函数,一般将它作为某模块的属性,而不是类的属性. 作为类对象属性的函数会被隐式转换成一个非绑定的方法调用(参见食谱5.13). 因此,如果想将函数作为类(属性)静态函数调用,无需隐式转换, 需要绑定次函数到另外一个类型的的可调用对象,然后绑定此callable对象作为类的属性. Python 2.2提供了新的内置类型 staticmethod,提供需要的绑定操作. 食谱中展示了如何使用staticmethod以及如何在python早期版本中使用一个小的辅助类完成同样功能。
As the recipe shows, you normally define the function that will become a static method with a def statement in the class body, and then immediately rebind the same name to the staticmethod object. You don't have to do it this way, though. You could simply write the following code outside of the class body:
如上面代码所示, 需要类的静态函数,一般在类中使用def语句定义此函数,然后马上绑定此函数名称到staticmethod对象。 不过,可以不必这样作,只要简单的在类外面使用如下代码:
def anotherfunction( ): print "Yes, you CAN do that" Greeter.peculiarmethodname = staticmethod(anotherfunction)
Unless you have a good reason to proceed in this way, such a noncustomary way of doing things will just confuse future readers of your code.
没有特别原因请不要这样做,这样不符合习惯的代码编写方式会使后来的阅读者(#译注:包括你自己)感到困惑。
In some languages (such as C++ or Java), static methods are also sometimes called class methods. However, the term class methods should be reserved for methods that belong to the class, in the same way that normal methods belong to the instance (i.e., for methods that receive the class object as their first implicit argument). Static methods in Python, as in C++, are little more than bland syntactical sugar for free-standing functions. See Recipe 5.8 for how to make real class methods (a la Smalltalk) in Python.
在一些语言中( 如 C++ 和Java ), 静态方法有时也被称为类方法. 然而, 名词类方法应该为属于类的方法(比如,接受类对象作为第一个隐式参数的方法)保留, 就像一般方法属于类的实例一样. Python中的静态函数, 和C++一样,差不多就是独立函数的混合语法糖衣. 食谱5.8中展示了如和构建真正的类函数(使用smalltalk风格)。
1.4. 参考 See Also
食谱5.8 食谱5.13