python学习笔记--3
(21)python中的self等价于c++中self指针和java、中的this参考
(22)python中类/对象和函数方法一样,区别只是一个额外的self变量,如:
class person:
defsayhi(self):
print'hello, how are you?'
p= person()
p.sayhi()
打印结果:hello, how are you?
(23)__init__方法在类的一个对象被建立时,马上运行,用来为对象进行初始化,开始和结尾都是双下划线。该方法类似于c++、c#和java中的构造函数。__del__(self)在对象消逝的时候调用,即对象不再使用,它所占用的内存将返回给,需要自己指明del 。例如:
class person:
def__init__(slef,name):
self.name= name
def__del__(self):
print'%s says bye.' % self.name
defsayhi(self):
print'hello, my name is',self.name
p = person('lvzhang')
p.sayhi()
打印结果:
hello, my name is lvzhang
如果想删除对象,在运行界面输入del p即可,此时再输入p.sayhi会报错
(24)类的变量由一个类的所有对象共享使用,只有一个类变量的拷贝,所有当某个对象对类的变量做类修改时,会反映到其他实例上;对象的变量由类的每个对象拥有。因此每个对象有自己对这个域的一份拷贝,即它们不是共享的。
(25)在python中也可以使用继承,在子类中对于基类中的构造函数,但是python不会自动调用基本类的构造函数。需要专门调用它。例如:
class
schoolmember
:
'''representsany school member.'''
def
__init__
(self,name, age):
self.name= name
self.age= age
print
'(initialized schoolmember: %s)'
% self.name
def
tell
(self):
'''tellmy details.'''
print
'name:"%s" age:"%s"'
% (self.name, self.age),
class
teacher
(schoolmember):
'''representsa teacher.'''
def
__init__
(self,name, age, salary):
schoolmember.__init__(self,name, age)
self.salary= salary
print
'(initialized teacher: %s)'
% self.name
def
tell
(self):
schoolmember.tell(self)
print
'salary: "%d"'
% self.salary
class
student
(schoolmember):
'''representsa student.'''
def
__init__
(self,name, age, marks):
schoolmember.__init__(self,name, age)
self.marks= marks
print
'(initialized student: %s)'
% self.name
def
tell
(self):
schoolmember.tell(self)
print
'marks: "%d"'
% self.marks
t = teacher(
'mrs. shrividya'
,
40
,
30000
)
s = student(
'swaroop'
,
22
,
75
)
print
# prints a blank line
members = [t, s]
for
member
in
members:
member.tell()
# works for both teachers and students
打印结果
:
(initialized schoolmember: mrs. shrividya)
(initialized teacher: mrs. shrividya)
(initialized schoolmember: swaroop)
(initialized student: swaroop)
name:"mrs. shrividya" age:"40" salary: "30000"
name:"swaroop" age:"22" marks: "75"
(26)你可以通过创建一个file类的对象来打开一个文件,分别使用file类的read、readline或write方法来恰当地读写文件。对文件的读写能力依赖于你在打开文件时指定的模式。最后,当你完成对文件的操作的时候,你调用close方法来告诉python我们完成了对文件的使用,例如:
poem =
'''\
programming is fun
when the work is done
if you wanna make your work also fun:
usepython!
'''
f =
file
(
'poem.txt'
,
'w'
)
# open
for 'w'riting
f.write(poem)
# write text to file
f.close()
# close the file
f =
file
(
'poem.txt'
)
# if no mode is specified, 'r'ead mode isassumed by default
while
true
:
line =f.readline()
if
len
(line) ==
0
:
# zero
length indicates eof
break
print
line,
# notice comma toavoid automatic newline added by python
(逗号用来消除自动换行)
f.close()
# close the file
打印结果:
programming is fun
when the work is done
if you wanna make your work also fun:
use python!
(27)python提供类一个标准的模块,成为pickle,使用它可以在一个文件中储存任何python对象,也称为持久地储存对象。还有一个cpickle模块(编写的,比pickle快1000倍),储存与取储存例如:
import
cpickle
as p
#import pickle as p
shoplistfile =
'shoplist.data'
# the name of the file where we willstore the object
shoplist = [
'apple'
,
'mango'
,
'carrot'
]
# write to the file
f =
file
(shoplistfile,
'w'
)
p.dump(shoplist, f)
# dump the object to a file
f.close()
del
shoplist
# remove theshoplist
# read back from the storage
f =
file
(shoplistfile)
storedlist = p.load(f)
print
storedlist
打印结果:
['apple', 'mango', 'carrot']
(28)对于异常有try……except处理异常,可以使用raise语句印发异常。当读一个文件的时候,希望无论异常发生与否double关闭文件,使用finally块来实现,try……finally。
(29)python有丰富的标准库。比如 sys模块,os模块等等
(30)python更多用法,比如:
1.使用列表综合
listone = [
2
,
3
,
4
]
listtwo = [
2
*i
for
i
in
listone
if
i
>
2
]
print
listtwo
打印结果:
[6, 8]
2.在函数中接收元组和列表
def powersum(power,*args):
total= 0
fori in args:
total+=pow(i,power)
returntotal >>> powersum(2, 3, 4)
打印结果为:
25(3的平方加上4的平方)
>>> powersum(2, 10)
100
3.使用lambda语句用来创建新的函数对象,并且在运行时返回它们,注意即便是print语句也不能用在lambda形式中,只能使用表达式
def
make_repeater
(n):
return lambda
s: s*n
twice = make_repeater(
2
)
print
twice(
'word'
)
print
twice(
5
)
打印结果:
wordword
10
4. exec语句用来执行储存在字符串或文件中的python语句,如:
>>> exec 'print "helloworld"'
hello world
5. eval语句用来计算存储在字符串中的有效python表达式,如:
>>> eval('2*3')
6. assert语句用来声明某个条件是真的,如果你非常确信某个你使用的列表中至少有一个元素,而你想要检验这一点,并且在它非真的时候引发一个错误,那么assert语句是应用在这种情形下的理想语句。当assert语句失败的时候,会引发一个assertionerror:
>>> mylist = ['item']
7. repr函数用来取得对象的规范字符串表示,如: >>> i = []
>>> assert len(mylist) >= 1
>>> mylist.pop()
'item'
>>> assert len(mylist) >= 1
traceback (most recent call last):
file "
assertionerror
>>> i.append('item')
>>> `i`
"['item']"
>>> repr(i)
"['item']"
下一篇: MySQL中的函数