Python类方法,静态方法

class Student:
    company = 'LOL'
    @classmethod
    def say_company(cls):
        print(cls.company)

Student.say_company()
与类方法界限不清晰,可以访问类,只是不通过CLS


class
Student: company = 'LOL' @staticmethod def add(a,b): print(Student.company) print("{0}+{1}={2}".format(a,b,a+b)) Student.add(1,2)

两种方法多不能访问实例变量。

原文地址:https://www.cnblogs.com/zzm-blog/p/10720333.html