码迷,mamicode.com
首页 > 编程语言 > 详细

Python进阶---面向对象第三弹(进阶篇)

时间:2017-07-09 23:15:27      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:teacher   主程   pass   mysql   sub   清理   object   super   python   

 Python对象中一些方法

一、__str__

class Teacher:
def __init__(self,name,age):
self.name=name
self.age=age
self.courses=[]

def teach(self):
print(‘%s teach‘ %self.name)

def __str__(self):
return ‘<name:%s age:%s>‘ %(self.name,self.age)

class Course:
def __init__(self,name,price,period):
self.name=name
self.price=price
self.period=period
def __str__(self):
return ‘《name:%s price:%s period:%s》‘ %(self.name,self.price,self.period)

# egon=Teacher(‘egon‘,18)
# print(egon) #egon.__str__()
# print(egon) #egon.__str__()

二、__del__用法

import time
# class Foo:
# def __init__(self,x):
# self.x=x
# print(‘connect mysql‘) #conn=abcdef(‘192.168.1.10‘,3306)
#
# def __del__(self):
# ‘‘‘做一些与这个对象有关的清理操作‘‘‘
# # conn.close()
# # file.close()
# print(‘====>‘)
# f=Foo(10)
# del f #f.__del__()
# time.sleep(3)
# print(‘主程序‘)

三、item用法

可以实现类于dic[‘a‘]这样的方法

# l=[‘a‘,‘b‘,‘c‘]
# dic={‘a‘:1}
#
# print(l[1])
# print(dic[‘a‘])

class Foo:
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
def __getitem__(self, item):
# print(self,item,type(item))
# return getattr(self,item)
return self.__dict__[item]
def __setitem__(self, key, value):
# setattr(self,key,value)
self.__dict__[key]=value

def __delitem__(self, key):
# delattr(self,key)
self.__dict__.pop(key)

def __len__(self):
return 10
f=Foo(‘egon‘,18,‘male‘)
# print(f.name) #f[‘name‘]
# print(f.age) #f[‘age‘]
# print(f.sex) #f[‘sex‘]

# print(f[‘name‘])

# f[‘name‘]=‘egon_nb‘
# print(f.__dict__)
# del f[‘name‘]
# print(f.__dict__)

print(len(f))

四、isinstance和issubclass

isinstance(obj,cls)检查是否obj是否是类 cls 的对象

class Foo(object):
pass

obj = Foo()

isinstance(obj, Foo)
issubclass(sub, super)检查sub类是否是 super 类的派生类


class Foo(object):
pass

class Bar(Foo):
pass

issubclass(Bar, Foo)

五、反射

class Teacher:
# school=‘oldboy‘
# def __init__(self,name,age):
# self.name=name
# self.age=age
#
# def teach(self):
# print(‘%s teach‘ %self.name)


# print(Teacher.school)
# print(Teacher.__dict__[‘school‘])

# print(hasattr(Teacher,‘school‘))

# print(getattr(Teacher,‘school‘))
# print(getattr(Teacher,‘solasdf‘,None))


# Teacher.x=123
# setattr(Teacher,‘x‘,123)
# print(Teacher.x)


# delattr(Teacher,‘school‘)
# print(Teacher.school)


#对象
# t=Teacher(‘egon‘,18)
# print(hasattr(t,‘name‘))#判断对象是否有name属性(“以字符串的方式”)

# print(getattr(t,‘name‘))#获取对象的name属性(“以字符串的方式”)

# setattr(t,‘sex‘,‘male‘)#修改对象的sex属性(“以字符串的方式”)
# print(getattr(t,‘sex‘))
#
# print(t.__dict__)
# delattr(t,‘name‘)
# print(t.__dict__)

# t.teach()
# print(t.school)

# print(getattr(t,‘teach‘))
# print(getattr(t,‘school‘))
# t.school=‘hahahahahahahahahhahahahahahhahahahahahahh‘
# print(t.__dict__)

Python进阶---面向对象第三弹(进阶篇)

标签:teacher   主程   pass   mysql   sub   清理   object   super   python   

原文地址:http://www.cnblogs.com/niubin/p/7143326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!