learn python the hard way(笨办法学python) 练习40 类的用法
2016-01-25 11:09
253 查看
class Song(object): #创建一个类Song,它是对象的一种 Make a class named Song that is-a object def __init__(self, lyrics): #类Song有一个__init__接受self和lyrics作为参数 class Song has-a __init__ that takes self and lyrics parameters self.lyrics = lyrics #从self中获取lyrics属性,并将其设为lyrics From self get the lyrics attribute and set it to lyrics def sing_me_a_song(self): #类Song有一个函数名称为s..,它接受self作为参数 class Song has-a function named s..that takes self parameters for line in self.lyrics: #for循环 print line happy_bday = Song(["Happy birthday to you", #将happy_bday设为类Song的一个实例 "I don't want to get sued", "So I'll stop right there"]) bulls_on_parade = Song(["They rally around tha family", #设为类Song的一个实例 "With pockets full of shells"]) happy_bday.sing_me_a_song() #从happy_bday中找到sing_me_a_song函数,并调用它。 bulls_on_parade.sing_me_a_song() #从bull_on_parade中找到sing_me_a_song函数,并调用它。
相关文章推荐
- 在python3.3后urllib2已经不能再用,只能用urllib.request来代替
- python 常用模块
- python list解析, map,生成器表达式
- python 函数
- python废话五:python获取本机信息
- 关于python-NameError: global name 'platform' is not defined
- Python开发基础-day1
- Python字符编码详解
- 详解Python验证码识别
- Python--多进程
- python废话四:python的import引入模块
- python 帮助和导入路径
- 谈谈Python进行验证码识别的一些想法
- Python网站验证码识别
- Python中变量的作用域(variable scope)
- 基于Python Shell获取hostname和fqdn释疑
- python-dict
- 将Sublime Text 3设置为Python全栈开发环境
- 12306的变态验证码算得了什么?我有Python神器!
- Python 初学者的最佳学习资源