EX40

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

EX40的相关文章

ex40.py

1 class song(object): 2 3 def __init__(self, lytics): 4 self.lyrics = lytics 5 6 def sing_me_a_song(self): 7 for line in self.lyrics: 8 print (line) 9 def why_you_poor(self): 10 print ("you konw it!,) 11 12 happy_bday = song(["Happy birthday to

ex40 模块,对象和类

很难理解,目前一知半解,希望通过后边的练习加深理解. 1 #-*- coding: UTF-8 -*- 2 ''' 3 import mystuff 4 mystuff.apple() 5 6 print mystuff.tangerline' 7 ''' 8 class Song(object): 9 10 def __init__(self,words):#这里注意下划线是左右各两条,否则会出错:另外words在这叫形参. 11 self.lyrics = words 12 def sing

20170928习题

ex36 #分支和函数from sys import exit #导入system模块中的exit函数 def gold_room(): #定义金子房间的函数 print("this room is full of gold,how much do you take?") next = input(">") #输入内容 # if "0" in next or "1" in next: # how_much = int(n

MOOS学习笔记——多线程

/* * A simple example showing how to use a comms client */ #include "MOOS/libMOOS/Comms/MOOSAsyncCommClient.h" #include "MOOS/libMOOS/Utils/CommandLineParser.h" #include "MOOS/libMOOS/Utils/ConsoleColours.h" #include "MO

[IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果你觉得英文版看着累,当当网有中文版,也有电子版可以选择. 我试着将其中的代码更新到Python 3.同时附上一些自己的初学体会,希望会对你有帮助. 中文版有人把书名翻译为<笨办法学python>,其实我觉得叫做<学Python,不走寻常路>更有意思些. 作者的意思你可以在序言中详细了解