Python面向对象
1. Models,Packages and Program
1.1 models
通过以下两行代码,可查看可以调用模块的路径
import sys
sys.path
我的工作目录为上述结果中的第一个,假设我在这个目录下创建了report.py文件,具体内容如下:
def get_descrption():
from random import choice
possibilities = ['rain','snow','sleet','fog','sun','who knows']
return choice(possibilities)
def get_desc():
from random import choice
possibilities = ['rain','snow','sleet','fog','sun','who knows']
return choice(possibilities)
现在我在jupyter notebook里调用函数,只需要输入如下代码:
import report
report.get_descrption()
1.2 packages
在python目录下看见__init__.py file
通常是包
一个包下有很多模块,模块是一个文件,包是多个文件组成的目录
调用方式:from package名 import model名
2. 文件I/O
open, write, read,close
text = '''\
frist line
second line
third line
end
'''
fout = open('new_file.txt','w')
ret = fout.write(text)
print("open value of write()=",ret)
fout.close()
fout = open('new_file.txt','r')
all_lines = fout.readlines()
print(all_lines)
fout.close()
读写字节型的文件时,需要加‘wb’,‘rb’
3. 类
3.1 定义类
class Person:
pass
someone = Person()
type(someone)
class Person:
def __init__(self,name,gender):
self.name = name
self.gender = gender
ed = Person('Edward','male')
print('Name:',ed.name)
print('Gender:',ed.gender)
class Person:
def __init__(self,name,gender):
self.name = name
self.gender = gender
def say(self):
print("Hi,I'm " + self.name + ",it's nice to meet you!")
ed = Person("jashou","male")
ed.say()
3.2 类的继承
- 子类继承了父类的所有属性
class MDPerson(Person):
pass
md_ed = MDPerson("Jack","male")
md_ed.say()
md_ed.name
md_ed.gender
- 父类不能调用子类的特殊功能
class MDPerson(Person):
def diagnose(self):
print("You need some treatment!")
new_ed = MDPerson("Jack","male")
new_ed.say()
new_ed.diagnose()
输出:Hi,I'm Jack,it's nice to meet you!
You need some treatment!
new_ed = Person("Jack","male")
new_ed.say()
new_ed.diagnose()
输出:Hi,I'm Jack,it's nice to meet you!
AttributeError: 'Person' object has no attribute 'diagnose'
3.3 super()
class MDPerson(Person):
def __init__(self,name,gender,dept = "Cardiac Surgry"):
super().__init__(name,gender)
self.name = 'Doctor '+ self.name
self.dept = dept
def say(self):
print("Hi,I'm %s from %s department,how can I help you?"%(self.name,self.dept))
ed = MDPerson("Joshua","male")
ed.say()
输出:Hi,I'm Doctor Joshua from Cardiac Surgry department,how can I help you?
3.4 多态
class Quote():
def __init__(self,person,words):
self.person = person
self.words = words
def who(self):
return self.person
def says(self):
return self.words + '.'
class QuestionQuote(Quote):
def says(self):
return self.words + '?'
class ExclamationQuote(Quote):
def says(self):
return self.words + '!'
def who_says(obj):
print(obj.who(),'says:',obj.says())
q1 = Quote('Ed','Normal Quote')
q2 = QuestionQuote('Ed','Question Quote')
q3 = ExclamationQuote('Ed','Exclamation Quote')
quotes = [q1,q2,q3]
for q in quotes:
print(q.says())
class Ed():
def who(self):
return 'Ed'
def says(self):
return "Hi,I'm Edward:"
ed = Ed()
print(ed.says())
quotes = [q1,q2,q3,ed]
for q in quotes:
print(q.says())
Ed.says(q3)
友好地输出
class Quote():
def __init__(self,person,words):
self.person = person
self.words = words
def __str__(self):
return "Quote('" + self.person + "','" + self.words + "')"
def who(self):
return self.person
def says(self):
return self.words + '.'
q1 = Quote('Ed','Normal Quote')
print(q1)
输出:Quote('Ed','Normal Quote')