1、OOP是python的一种抽象方法。
2、OPP最好主要的两个方面:类和实例。
3、定义类
>>> class MyData(object): x = 4
4、根据类创建实例
>>> a = MyData()
5、定义类的方法
>>> class MyData(object):... def pstar(self):... print '*' * 40...>>> a = MyData()>>> a.pstar()****************************************
6、调用方法
a = MyData()
a.x
7、创建类的例子
#!/usr/bin/env pythonclass AddBookEntry(object):def __init__(self, nm, em):self.name = nmself.email = emtom = AddBookEntry('tom', 'tom@qq.com')print tom.nameprint tom.emailalice = AddBookEntry('alice', 'alice@163.com')print alice.nameprint alice.email

#!/usr/bin/env pythonclass AddBookEntry(object):def __init__(self, nm, em):self.name = nmself.email = emdef getEmail(self):print 'get email'return self.emaildef updateEmail(self, newem):self.email = newemprint 'email upadte ...'tom = AddBookEntry('tom', 'tom@qq.com')alice = AddBookEntry('alice', 'alice@163.com')print alice.emailprint alice.getEmail()alice.updateEmail('alice@qq.com')print alice.getEmail()

8、 创建一个子类
#!/usr/bin/env pythonclass AddBookEntry(object):def __init__(self, nm, em):self.name = nmself.email = emdef getEmail(self):print 'get email'return self.emaildef updateEmail(self, newem):self.email = newemprint 'email upadte ...'class EmplAddrBookEntry(AddBookEntry):def __init__(self, id, nm, em, ph):AddBookEntry.__init__(self, nm, em)self.id = idself.phone = phdef getPhone(self):return self.phonealice = EmplAddrBookEntry('0001', 'alice', 'alice@qq.com', '12345678901')print alice.getEmail()print alice.getPhone()

#!/usr/bin/env pythonclass Info(object):def __init__(self, em, ph):self.email = emself.phone = phdef getEmail(self):return self.emaildef updateEmail(self, newem):self.email = newemclass Contacts(object):def __init__(self, nm, em, ph):self.name = nmself.info = Info(em, ph)alice = Contacts('alice', 'alice@qq.com', '12345678901')print alice.info.getEmail()alice.info.updateEmail('alice@163.com')print alice.info.getEmail()

对任何类C
C.__name__ 类C的名字(字符串)
C.__doc__ 类C的文档字符串
C.__bases__ 类C的所有父类构成的元组
C.__dict__ 类C的属性
C.__module__ 类C定义所在的模块(1.5版本新增)
C.__class__ 实例C对就的类(仅新式类中)
<type 'type'>类和类型统一了
定义一个类来计算这个假想旅馆租房费用。__init__()构造器对一些实例属性进行初始化。
c alcTotal()方法用来决定是计算每日总的租房费用还是计算所有天全部的租房费。
#!/usr/bin/env pythonclass HoteCacl(object):def __init__(self, rt, sales = 0.085, rm = 0.1):self.rt = rtself.sales = salesself.rm = rmdef caclTotal(self, days = 1):return self.rt * (1 + self.sales + self.rm) * dayssfo = HoteCacl(200)print sfo.caclTotal()print sfo.caclTotal(3)
核心笔记:self是什么?
self变量用于在类实例方法中引用方法所绑定的实例。因为方法的实例在任何方法调用中总是作为第一个参数传递的,self被选中用来代表实例。你必须在方法声明中放在self(你可能已经注意到了这点),但可以在方法中不使用实例(self)。如果你的方法中没有用到self,那么请考虑创建 一个常规函数,除非你有特别的原因。毕竟,你的方法代码没有使用实例,没有与类关联其功能,这使得它看起来更像一个常规函数。在其它面向对象语言中,self可能被称为this。
11、私有化。只能在对象内部使用的属性、方法
#!/usr/bin/env pythonclass MyData(object):def __init__(self, nm):delf.__name = nma = MyData('alice')print a.__name //将会报错
为了能访问对象的属性,只能在类的定义里面定义方法
#!/usr/bin/env pythonclass MyData(object):def __init__(self, nm):self.__name = nmdef getName(self):return self.__namea = MyData('alice')print a.getName()
若在PYTHON中以下划线开头的变量显示为 _类名__变量名
12、判断某一个类是否为另一个类的子类
issubclass(EmplAddrBook, AddrBook) //返回True
13、查看某个类的父类,可以使用特殊属性:
EmplAddrBook.__bases__
14、判断某一个对象是否为一个类的实例
>>> isinstance(3, int)True>>> isinstance(tom, AddrBook)True
15、多个超类
#!/usr/bin/env pythonclass A(object):def hello(self):print 'hello from A'class B(object):def greet(self):print 'greeting from A'class C(A, B):passc = C()c.hello()c.greet()
二、高级功能模块
1、使用ftplib下载文件
#!/usr/bin/env pythonimport ftplibimport sysimport socketdef getfile():try:f = ftplib.FTP('localhost')except (socket.gaierror, socket.error), e:print 'Error: ', esys.exit(1)f.login('ftp')print f.getwelcome()f.cwd('pub')print 'change dir: ', f.pwd()print 'file list: 'f.dir()fname = raw_input('file to download: ')f.retrbinary('RETR %s' % fname, open(fname, 'w').write)f.quit()def test():getfile()if __name__ == '__main__':test()