1 # this goes in mystuff.py 2 def apple(): 3 print "I AM APPLES!" 4 5 # this is just a variable 6 tangerine = "Living reflection of a dream." # 以上资料保存为mystuff.py
1 import mystuff 2 3 mystuff.apple() 4 print mystuff.tangerine # 以上资料保存为ex401.py(名字可以随意选取,这个文件的主要目的在于用import mystuff来进入第一个文件内
1 mystuff = {‘apple‘: "I AM APPLES!"} 2 print mystuff[‘apple‘] # 单独一个文件,跟第1、2无关联,保存文件ex40.py
上图为mystuff文件内容
1、[] 中括号,代表List列表数据类型; {} 大括号,代表dict字典数据类型,用冒号:分开键&值,用逗号,分开组
2、
1 mystuff[‘apple‘] # get apple from dict 2 mystuff.apple() # get apple from the module 3 mystuff.tangerine # same thing, it‘s just a variable
第一个是直接从字典内抽取apple键;第二是从模块中抽取apple键,第三同理,只不过它是个变量
1 class Song(object): 2 3 def __init__(self, lyrics): 4 self.lyrics = lyrics 5 def sing_me_a_song(self): 6 for line in self.lyrics: 7 print line 8 9 happy_baby = Song(["Happy birthday to you", 10 "I don‘t want to get sued", 11 "So I‘ll stop right there"]) 12 13 bulls_on_parade = Song(["They rally around the family", 14 "With pockets full of shells"]) 15 16 happy_baby.sing_me_a_song() 17 18 bulls_on_parade.sing_me_a_song()
1、__init__:这个下行线是由_两个组成
此章节完全没有学懂,先过,后面重新再看
时间: 2024-10-27 18:37:31