(21)python中的self等价于c++中self指针和java、c#中的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模块(c语言编写的,比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*ntwice = 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']
 >>> assert len(mylist) >= 1
 >>> mylist.pop()
 'item'
 >>> assert len(mylist) >= 1
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 AssertionError

7. repr函数用来取得对象的规范字符串表示,如:

>>> i = []
 >>> i.append('item')
 >>> `i`
 "['item']"
 >>> repr(i)
 "['item']"