模块
模块用import来调用,例如
from sys import argv
调用sys中argv模块
在模块里有多个def的函数
import调用全部或其中一个
类和对象
下面定义了一个类
1 class song(object): 2 3 def __init__(self, lyrics): 4 self.lyrics = lyrics 5 6 def sing_me_a_song(self): 7 for line in self.lyrics: 8 print line
类好比一个蓝图,一个骨架,填充不同的东西, 出来的就是不同的物品
__init__处接受传递的参数,lyrics来接受,传递给self.lyrics
sing_me_a_song() 对传入的参数进行处理
例:
1 class song(object): 2 3 def __init__(self, lyrics): 4 self.lyrics = lyrics 5 6 def sing_me_a_song(self): 7 for line in self.lyrics: 8 print line 9 10 happy_bday = song(["happy birthday to you", 11 "i don't want to get sused", 12 "so i'll stop right here"]) 13 happy_bday.sing_me_a_song()
运行结果