由于类的内容一片文章介绍不完,所以这篇文章我们来说一下类特殊成员方法
类的特殊成员方法实例
1.__doc__方法
class People:
'''我是一个People类''' #写在类的下面用''' '''包裹
def __init__(self,name,age): #传入name,age参数
#构造函数,几乎每个类都需要这样写
#在实例化时做一些类的初始化工作
self.name=name #实例变量(静态属性),作用域就是实例本身
self.age=age
def talk(self):
print('%s can talking with women.... he is %s years old'% (self.name,self.age))
print(People.__doc__) #打印类的描述信息
2.__module__和__class__
__module__表示当前操作的对象在哪个模块;__class__表示当前操作的对象的类是什么
当前目录下的lib.py中的代码class People(object):
def __init__(self):
print('我是一个lib模块')
实例引入from lib import People
r1=People()
print(r1.__module__) #打印lib,即输出模块,一个文件可以说是模块
print(r1.__class__) #打印<class 'lib.People'>,打印类
__import__
当前目录下的lib.py中的代码class People(object):
def __init__(self):
print('我是一个lib模块')
我们可以导入lib模块,但是我们不能导入”lib”模块,因为它是一个字符串,但是我么可以采用下面的方法导入
#from lib import lib
str=__import__('lib') #导入'lib'
obj=str.People()
但是官方建议我们采用下面的方式动态导入
import importlib
lib=importlib.import_module('lib')
lib.People()
3.__init__
构造方法,通过类创建对象时,自动触发
__del__
构造方法,当对象在内存中被释放时,自动触发执行,一般无需定义
4.__call__
对象后面加括号,触发执行。
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def talk(self):
print('%s can talking with women.... he is %s years old'% (self.name,self.age))
def __call__(self, *args, **kwargs): #参数是固定传入的
print('实例化类之后,在后面加上(),我就执行了')
r1=People('zjj',18) #我们可以这样实例化
r1() #在后面再加一个()就会报错,我们在类中加上__call__方法
5.__dict__
打印类或对象中的所有成员
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def talk(self):
print('%s can talking with women.... he is %s years old'% (self.name,self.age))
def __call__(self, *args, **kwargs):
print('实例化类之后,在后面加上(),我就执行了')
print(People.__dict__) #打印类里所有的属性,不包括实例属性
r1=People('zjj',18) #实例化
print(r1.__dict__) #打印所有的实例属性,不包括类属性
6.__str__
如果一个类中定义了__str__方法,那么在打印对象时,默认输出该方法的返回值
class People(object):
def __init__(self,name,age):
self.name=name
self.age=age
def talk(self):
print('%s can talking with women.... he is %s years old'% (self.name,self.age))
def __str__(self): #在没有加入__str__方法之前,我们打印实例化后的对象,就会显示return
return 'name:%s\nage:%s'% (self.name,self.age)
r1=People('zjj',18)
print(r1)
7.__getitem__、__setitem__、__delitem__
用于索引操作,字典,分别表示获取、设置、删除数据
class People(object):
def __init__(self):
self.data={}
def __getitem__(self, item):
print('__getitem__',item)
return self.data.get(item)
def __setitem__(self, key, value):
print('__setitem__',key,value)
self.data[key]=value
def __delitem__(self, key):
print('__delitem__',key)
r1=People()
r1['name']='zjj' #执行__setitem__
s=r1['wff'] #执行 __getitem__
del r1['wffffff']
class People(object):
def __init__(self):
print('Hello World')
r1=People() #对类实例化
print(type(r1)) #我们可以用type来查看实例的类型
print(type(People)) #People是来自与type
所以说,类默认是由type类实例化产生的,我们可以用type来实现一个简单的类
def foo(self):
print('Hello World %s'% self.name)
def __init__(self,name,age):
self.name=name
self.age=age
People=type('People',(),{'talk':foo,'__init__':__init__})
#type第一个参数为类名,第二个:当前类的基类,第三个:类成员
print(type(People))
r1=People('zjj',18)
r1.talk()
类中有一个属性__metaclass__,用来表示该类由谁来实例化创建,所以,我们可以为__metaclass__设置一个type的派生类,从而查看类创建的过程
def foo(self):
print('Hello World %s'% self.name)
def __init__(self,name,age):
self.name=name
self.age=age
People=type('People',(),{'talk':foo,'__init__':__init__})
#type第一个参数为类名,第二个:当前类的基类,第三个:类成员
print(type(People))
r1=People('zjj',18)
r1.talk()
8、__metaclass__
用来定义这个类以怎样的形式被创建,实例待更新…
9、__new__
Python反射
反射包括hasattr(x,’y’)判断一个对象x里是否含有对应的’y’字符串方法,getattr(x,’y’)根据字符串去获取x对象里的对应的方法的内存地址,setattr(obj,’choice’,v)相当于obj.choice=v,delattr(x, ‘y’)相当于del x.y
class People(object):
def __init__(self,name):
self.name=name
def run(self):
print('%s is running......'% self.name)
r1=People('ZJJ') #实例化之后让用户选择调用方法
choice=input('请问你需要查询什么方法:').strip() #用户输入run,然后调用
#我们可以采用反射,先判断是否有这个方法
print(hasattr(r1,choice)) #先判断是否有,用户输入run返回True,否则就是False
print(getattr(r1,choice)) #有了之后我们选择去调用
getattr(r1,choice)()
def eat(self):
print('%s is eatting ...'% self.name)
class People(object):
def __init__(self,name):
self.name=name
def run(self):
print('%s is running......'% self.name)
r1=People('ZJJ') #实例化之后让用户选择调用方法
choice=input('请问你需要查询什么方法:').strip() #用户输入run,然后调用
if hasattr(r1,choice):
getattr(r1,choice)
else:
setattr(r1,choice,eat) #相当于r1.choice=eat
r2=getattr(r1,choice)
r2(r1)
声明:1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!