看《Python高级编程》看到个好玩的例子

  

最近在看《Python高级编程》,看到个让人会心一笑的例子,贴上来。

class MeanElephant(object):
    
    def __init__(self):
        self._people_to_kill = []
    
    def is_slapped_on_the_butt_by(self, name):
        self._people_to_kill.append(name)
        print 'Ouch!'
    
    def revenge(self):
        print '10 year later...'
        for people in self._people_to_kill:
            print 'Me kill %s' % people
            
if __name__ == '__main__':
    joe = MeanElephant()
    joe.is_slapped_on_the_butt_by('Tarek')
    joe.is_slapped_on_the_butt_by('Bill')
    joe.revenge()

嘿嘿,挺有意思。

另这本书还可以,推荐给大家看看。

😊 如果你喜欢这篇文章,也欢迎了解我的书: 《Python 工匠:案例、技巧与工程实践》 。它专注于编程基础素养与 Python 高级技巧的结合,是一本广受好评、适合许多人的 Python 进阶书。