87-OOP之必需掌握的magic

class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

    def __str__(self):
        return '《%s》' % self.title

    def __call__(self):
        print('《%s》is written by %s' % (self.title, self.author))

if __name__ == '__main__':
    py_book = Book('Core Python', 'Wesley', 800)  # 调用__init__()方法
    print(py_book)  # 调用__str__
    py_book()  # 调用__call__
    原文作者:凯茜的老爸
    原文地址: https://www.jianshu.com/p/014856315e38
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞