017: class, objects and instance: class method
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了017: class, objects and instance: class method相关的知识,希望对你有一定的参考价值。
类的方法
所谓类的方法,也就是,这个方法会绑定到一个类上面,实例化一个instance的时候,这个方法不会再重新生成 一份,它只有访问类级别的变量
它用@classmethod标签来标注这是一个class method.
class Book(object): num = 10 # instance method, will be bound to an object def __init__(self, title, price): self.title = title self.price = price # class method, will not be bound to an object @classmethod def display(cls): print("\n*******************************") print("this is a class method", cls.num) print("===============================\n") book = Book("Python Basic", 25) Book.display() book.display() print("\n*******************************") print("<class method essence>") print(Book.display) print(book.display) print("===============================\n")
执行结果为:
******************************* this is a class method 10 =============================== ******************************* this is a class method 10 =============================== ******************************* <class method essence> <bound method type.display of <class ‘__main__.Book‘>> <bound method type.display of <class ‘__main__.Book‘>> ===============================
以上是关于017: class, objects and instance: class method的主要内容,如果未能解决你的问题,请参考以下文章
AWS CSAA -- 04 AWS Object Storage and CDN - S3 Glacier and CloudFront
hausaufgabe--python 39 -- objects and class