os.system 输出命令结果到屏幕,返回命令执行状态
os.popen("dir").read()#会保存命令的执行结果输出
py2.7
commands
commands.getstatusoutput("dir")
#windows不好用
#linux可以
subprocess
==================================
面向对象
模板造对象
一.类的特性
1.封装
防止数据被随意修改
使外部程序不需要关注对象内部的构造(逻辑结构),只需要通过此对象对外提供的接口进行直接访问即可
class Dog(object):
def __init__(self,name,food):#构造函数,构造方法,==初始化方法
self.NAME = name
self.FOOD = food
self.__getj = 'big'
self.__test = ''
def sayhi(self):#类的方法(类的具体方法)
print("hello,ni shuo de dui.",self.NAME)
def get_getj(self):
return self.__getj
def eat(self):
print("%s is eating %s"%(self.NAME,self.FOOD))
def __del__(self):
print("del...run....")
d = Dog("a1",'haochide')#Dog(d,"a1") #实例化后产生的对象叫实例(当前类的实例)
d2 = Dog("a2",'pi')
d.sayhi()
d2.sayhi()
d.eat()
d2.eat()
print(d.get_getj())
print(d._Dog__test)
def shot2():
print("buhuia")
d.sayhi = shot2
d.sayhi()
# print(d.test)
2.继承
通过父类---》子类的方式 实现不同角色的共同点和不同点 以最小代码量的方式实现
类 ----》实例化----》实例对象
__init__构造函数
self.name = name #属性,成员变量,字段
def sayhi() #方法,动态属性
公有属性
在类里定义的属性,任何对象都可以访问
私有属性
__private_attr_name = value
def get_heart(self) #对外部提供只读访问接口
return self.__heart
r1._Role__heart 强制访问私有属性
析构方法
继承
多继承
class Person(object):
def __init__(self,name,age):
self.name = name
self.age = age
self.sex = 'male'
def talk(self):
print("person is talk",self.name)
class BlackPerson(Person):
def __init__(self,name,age):
Person.__init__(self,name,age)
print(self.sex)
def walk(self):
print("is walk")
b = BlackPerson('liusijia',10)
b.talk()
新式类vs经典类
SchoolMember.__init__(self,name,age,sex) #经典类写法
super(Teacher,self).__init__(name,age,sex)#新式类写法
class Person(object):
super(Person)
class Person:#经典类
ParentClass.__init__
class SchoolMember(object):
'''学校成员基类'''
member = 0
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
self.enroll()
def enroll(self):
'''注册'''
print("just encolled a new school member %s"%self.name)
SchoolMember.member +=1
def tell(self):
'''个人信息'''
print('------info:%s-----'%self.name)
for k,v in self.__dict__.items():
print('\t',k,v)
def __del__(self):
print("开除[%s]..."%self.name)
SchoolMember.member -=1
class School(object):
'''学校类'''
def open_branch(self,addr):
print("openning branch in %s"%addr)
self.addr = ""
class Teacher(SchoolMember,School):
'''讲师类'''
def __init__(self,name,age,sex,salary,course):
SchoolMember.__init__(self,name,age,sex) #经典类写法
super(Teacher,self).__init__(name,age,sex)#新式类写法
self.salary = salary
self.course = course
# self.enroll()
def teaching(self):
print("Teacher[%s]is teaching[%s]"%(self.name,self.course))
class Student(SchoolMember):
def __init__(self,name,age,sex,course,tuition):
SchoolMember.__init__(self,name,age,sex)
self.course = course
self.tuition = tuition
self.amount = 0
def pay_tuition(self,amount):
print("student[%s]has just paied [%s]"%(self.name,amount))
self.amount += amount
t1 = Teacher("a1",11,"F*M",3000,"python")
s1 = Student("a2",14,"NA","py",3000)
s2 = Student("a3",13,"NA","py",3000)
print(SchoolMember.member)
del s1
print(SchoolMember.member)
t1.tell()
s2.tell()
3.多态
静态方法
类方法
属性方法