python 静态类?

在java或者c#中都有静态类的概念,在python中有没有呢? java

答案是No,python中不存在静态类,在类中的方法第一个参数必须是self。仔细一想其实python根本就没有必要有静态类,py文件中直接写方法就是静态类了。 python

以上说法是错误的,python中存在静态类,并有staticmethod和classmethod的区分。但不知道这二者之间有什么区别:c#

以下示例代码:code

class Foo:
    @staticmethod
    def bar_staticmethod():
        print 'I am bar'
    @classmethod
    def bar_classmethod(cls):
        print 'I am bar with ' ,cls

Foo.bar_staticmethod()
Foo.bar_classmethod()
相关文章
相关标签/搜索