py_1
mudule(模块)
for sys import argv
引入模块,相当于调用库import aygv
案例1.文件的读取1
2
3
4
5from sys import argv
script,filename = argv
txt = open(filename) #open打开的对象赋予给txt
print "Here's your file %s:" %filename
print txt.read()object = open(filename)
文件对象的方法案例11
2
3
4
5close() 关闭文件
read() 读取文件
truncate() 截断.清空文件
readline() 一行
write(stuff) 将stuff写入文件案例2(文件复制)1
2
3
4
5
6
7
8
9
10
11
12from sys import argv
script,filename = argv
target = open(filename,"w")
target.truncate() #清空打开的文档
for i
line1=raw_input(">")
line2=raw_input(">")
line3=raw_input(">")
for i in [line1,line2,line3]:
target.write(i)
target.write("\n")
target.close() #关闭文件1
2
3
4
5
6
7
8
9
10
11
12
13from sys import argv
from os.path import exists
script,from_file,to_file = argv
in_file = open(from_file)
indata = in_file.read()
print "the input file is %d bytes long." %len(indata)
#判断拷贝至的文件是否存在
print "does the output file exists? $r."&exists(to_file)
out_file = open(to_file,"w")
out_file.write(indata) #写入数据
#关闭文件
out_file.close()
in_file.close()
function
- 通过
def
定义函数 *arg
代表接受所有参数,放进arg列表中
案例11
2
3
4
5
6
7
8
9def cheese_and_crackers(cheese_count,boxer_of_crackers):
print " 1: %d "%cheese_count,
print " 2: %d "%boxer_of_crackers
cheese_and_crackers(20,90)
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese,amount_of_crackers)
cheese_and_crackers(10+20,5+6)
cheese_and_crackers(amount_of_cheese+1,amount_of_crackers+1)
function and file
- fileobject.seek() 制定读取文件指针到指定位置
- fileobject.readline() 读取每一行,参数是读取字符的个数
- 案例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15from sys import argv
script,input_file = argv
def print_all(f):
print f.read()
def rewind(f):
f.seek(0);
def print_a_line(line_count,f):
print line_count,f.readline()
current_file = open(input_file)
print_a_line(current_file)
#seek(0)讲读取文件指针再次指向开头
rewind(current_file)
#假设文件有三行
for i in [1,2,3]:
print current_file.readline()函数返回值
1
2def add(a,b):
return a+b脚本语言中大多
else if
都是elif
python中
if
这种条件后面要加:
1
2
3
4
5
6
7a=int(raw_input(">"));b=int(raw_input(">"))
if a>b :
print "a>b"
elif a == b :
print "a=b"
else :
print "a<b"
数组:list
列表
- 初始化空列表
elements = []
range(begin,end,step)
可用来构造列表- list添加元素
elements.append(number)
stuff = {key:value}
map
在python中称为dictionary
stuff = {1:'wow','abc':'edf'}
- 删除其中的元素
del stuff['abc']
- 或者释放内存
del stuff
super().xxx()
调用父类函数
子类调用父类隐式动作时,python会回到类的层次结构中以固定的次序去检查父类,使用了方法解析顺序(method resolution order
)
- 隐式继承(
implicit inheritance
)1
2
3
4
5class Parent(object):
def implicit(self):
print "parent implicit"
class Child(Parent):
pass - 继承+重写方法
1
2
3
4
5
6
7
8
9
10
11
12class Parent(object):
def altered():
print "PARENT altered()"
class Child(Parent):
def altered(self):
print "before"
super(Child,self).altered()
print "after"
dad = Parent()
son = Child()
dad.altered()
son.altered() - 继承+组合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class Other(object):
def override(self):
print "OTHER override()"
def implicit(self):
print "OTHER implicit()"
def altered(self):
print "OTHER altered()"
class Child(object):
def __init__(self):
self.other = Other()
def implicit(self):
self.other.implicit()
def override(self):
print "CHILD override()"
def altered(self):
print "CHILD before other altered()"
self.other.altered()
print "CHILD after other altered()"
son= Child()
son.implicit()
son.override()
son.altered()
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!