classmethod方法简介 来源菜鸟教程

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class A(object):
    bar = 1
    def func1(self):  
        print ('foo') 
    @classmethod
    def func2(cls):
        print ('func2')
        print (cls.bar)
        cls().func1()   # 调用 foo 方法
 
A.func2()               # 不需要实例化

输出如下:我们可以看到 类里面的方法没有self参数,且不需要实例化就可以调用类的属性和方法
func2
1
foo

猜你喜欢

转载自blog.csdn.net/qq_41000421/article/details/85279306
今日推荐