官方解释

  • @classmethod

一个类方法把类自己作为第一个实参, 就像一个实例方法把实例自己作为第一个实参.

语法格式:

class C:
    @classmethod
    def f(cls, arg1, arg2, ...): ...

官方文档链接

  • @staticmethod

静态方法不会接收隐式的第一个参数.

语法格式:

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

官方文档链接

特性

相同点
  • 可不实例化类直接调用
import time

class School(object):
    school_name = "A_Tester_School"
    address = "China_Sichuan"

    def __init__(self, student_name):
        self.student_name = "a student of A_Tester_School, name is: {} ".format(student_name)

    def age(self, birthday):
        return time.localtime().tm_year - birthday

    @classmethod
    def get_school(cls):
        return cls.school_name, cls.address

    @staticmethod
    def get_address():
        return "hello world."

# 实例调用
my_school = School("Hoo")
print(my_school.age(1991))
print(my_school.get_school())
print(my_school.get_address())
print("-"*50)
# 不实例化调用
print(School.get_school())
print(School.get_address())

# 28
# ('A_Tester_School', 'China_Sichuan')
# hello world.
# --------------------------------------------------
# ('A_Tester_School', 'China_Sichuan')
# hello world.
不同点
类别 @classmethod @staticmethod
语法格式 至少一个参数,第一个参数为类本身 可无参数

个人理解

  • 二者区别
  1. staticmethod装饰的函数就是一个普通的函数, 本质上与类没有半毛钱关系.所以无法使用类的局部变量(如:School类中的school_name), 无法操作类等.
  2. classmethod装饰的函数第一个入参为类本身, 因此可操作类, 包括引用类的staticmethod装饰的函数.
class ChildSchool(School):
    @classmethod
    def get_student_info(cls, student_name, birthday):
        my_school = cls(student_name)  # 实例化类
        age1 = my_school.age(birthday)  # 调用实例方法
        print("school_name: {}".format(cls.school_name))  # 不实例类, 调用类局部变量
        print("address: {}".format(my_school.address))  # 实例类, 调用类局部变量. 此处my_school可换成cls
        print("age: {}".format(age1))
        print("introduction: {}".format(my_school.student_introduction))  # 调用实例属性
        print("this a error address: {}".format(cls.get_address())) # 调用类静态方法

ChildSchool.get_student_info("Hoo", 1991)

# school_name: A_Tester_School
# address: China_Sichuan
# age: 28
# introduction: a student of A_Tester_School, name is: Hoo
# this a error address: hello world.

最新文章

  1. iOS UITableView 与 UITableViewController
  2. ASP.NET MVC 基于角色的权限控制系统的示例教程
  3. 查询Sql Server Agent 的job的执行情况。
  4. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
  5. grunt安装失败处理
  6. Rop 文件上传解决思路
  7. express源码剖析2
  8. POJ-2296 Map Labeler 2sat
  9. MyBatis 基本数据类型条件判断问题
  10. ThinkPhp知识大全(非常详细)
  11. postman Installation has failed: There was an error while installing the application. Check the setup log for more information and contact the author
  12. nginx安装配置并布置网站
  13. 2018面向对象程序设计(Java)第17周学习指导及要求
  14. 【JavaScript】js 中一些需要注意的问题
  15. 使用vue+webpack打包时,去掉资源前缀
  16. doPost方法不支持 a 标签和地址栏直接输入地址访问
  17. sql的主键,int类型,自增,自动编号到了规定最大数,接下来数据库会怎么做
  18. CentOS 6.7下配置 yum 安装 Nginx。
  19. 递归遍历嵌套结构(多层List)中的元素 ------Python
  20. configChanges

热门文章

  1. 防抖(debounce)和节流(throttle)
  2. laya2d 与 cad 之间的坐标转换
  3. RedHat 6.5换源
  4. Linux配置部署_新手向(一)——CentOS系统安装
  5. C#中appium自动化执行移动命令mobile:shell用法
  6. Python day01 课堂笔记
  7. int string类型互转
  8. Linux任务调度(8)
  9. Sqoop数据迁移工具的使用
  10. python画混淆矩阵(confusion matrix)