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

Type: 技术相关 - Posted at: 2010/08/20 11:57
Tags : 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()

嘿嘿,挺有意思。

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

<
>
1# 棉花糖   said this at 2010-08-20 13:08 : [Quote]
就看明白kill来kill去的,you want to kill me?
2# hef   said this at 2010-08-20 22:45 : [Quote]
暴力倾向... 不解释
3# Admin   said this at 2010-08-21 09:50 : [Quote]
Quoted 棉花糖:

就看明白kill来kill去的,you want to kill me?


这个类叫MeanElephant(小心眼的大象),这个类呢有一个列表(_people_to_kill)记录着要杀的人。
每次它被别的大象用角顶一次的时候,他就会记仇(_people_to_kill会加入这个大象的名字),想着要杀掉这些大象,后来过了十年,他就把顶过他的大象都杀掉了(调用revenge方法,遍历_people_to_kill,一个个杀掉...)。
= =
4# hehe   said this at 2010-10-29 08:52 : [Quote]
Quoted 棉花糖:

就看明白kill来kill去的,you want to kill me?

Quoted Admin:

这个类叫MeanElephant(小心眼的大象),这个类呢有一个列表(_people_to_kill)记录着要杀的人。
每次它被别的大象用角顶一次的时候,他就会记仇(_people_to_kill会加入这个大象的名字),想着要杀掉这些大象,后来过了十年,他就把顶过他的大象都杀掉了(调用revenge方法,遍历_people_to_kill,一个个杀掉...)。
= =


兄弟对程序的功能性解释没有问题,问题是对程序中的英文理解大大的有问题。

大象有角吗?
大象是去杀了那些拍了它的屁屁的人的
5# Admin   said this at 2010-10-29 09:48 : [Quote]
Quoted Admin:

这个类叫MeanElephant(小心眼的大象),这个类呢有一个列表(_people_to_kill)记录着要杀的人。
每次它被别的大象用角顶一次的时候,他就会记仇(_people_to_kill会加入这个大象的名字),想着要杀掉这些大象,后来过了十年,他就把顶过他的大象都杀掉了(调用revenge方法,遍历_people_to_kill,一个个杀掉...)。
= =

show all quoted

Quoted hehe:

兄弟对程序的功能性解释没有问题,问题是对程序中的英文理解大大的有问题。

大象有角吗?
大象是去杀了那些拍了它的屁屁的人的


嘿嘿,的确是弄错了。
Name:  *   Remember me
E-mail:  *  Not public, supported by Gravator
Website:      Not required
Question:
4 + 1 = *
Content: