标签:
在Python的世界里,实际上class也是对象,object也是对象,因此这里加了一个概念,实例
1. 实例方法
所谓的实例方法,也就是,这个方法会绑定到一个instance上面,这个方法一般是需要访问这个instance的数据。
该实例方法,类是依然存在一份的方法定义的,只是实例化一个类的时候,也会重新生成一个方法定义,和类的方法分别存储在不同的地址。
class Book(object): # instance method, will be bound to an object def __init__(self, title, price): self.title = title self.price = price # instance method, will be bound to an object def display(self): print("\n*******************************") print("BOOK DETAILS:") print("Title is:", self.title) print("Price is:", self.price) print("===============================\n") book = Book("Python Basic", 25) book.display() Book.display(book) print("\n*******************************") print("<instance method essence>") print(Book.display) print(book.display) print("===============================\n")
运行结果:
******************************* BOOK DETAILS: Title is: Python Basic Price is: 25 =============================== ******************************* BOOK DETAILS: Title is: Python Basic Price is: 25 =============================== ******************************* <instance method essence> <function Book.display at 0x02275108> <bound method Book.display of <__main__.Book object at 0x022348F0>> ===============================
016: class, objects and instance: method
标签:
原文地址:http://www.cnblogs.com/jcsz/p/5155041.html