一、系统函数__init__() 初始化类函数
功能:
使用该__init__()
类系统方法可以在创建类的实例化对象时,可以初始化对象 —— 传递参数(该类的全局变量),参数可以是函数对象,也可以是各种数据类型的变量
eg:
# test.py |
|
class System_Function: |
|
def __init__(self, function_or_varible): |
|
self.function_or_varible = function_or_varible |
|
print("init(初始化) class System_Function: {0}\n".format(self.function_or_varible)) |
|
# 初始化类对象 |
|
init_args = 'init_test' |
|
System_Function(init_args) |
|
程序输出显示:
init(初始化) class System_Function: init_test |
二、系统函数__call__() 调用对象函数
call
n. 打电话,通话;喊叫,呼叫;需求,需要;号召,呼吁;执行子程序的命令;
功能:
本质是将一个类变成一个函数
Python 类中一个非常特殊的实例方法,即
__call__()
。该方法的功能类似于在类中重载()
运算符,使得类实例对象可以像调用普通函数那样,以“对象名()
”的形式使用。
对于可调用对象,实际上“名称()”可以理解为是“
名称.__call__()
”的简写。仍以上面程序中定义的call_obj
实例对象为例,其最后一行代码还可以改写为如下形式:
obj.__call__("obj.__call__","__call__()") |
具体代码演示:
# test.py |
|
class System_Function: |
|
def __init__(self, function_or_varible): |
|
self.function_or_varible = function_or_varible |
|
print("init(初始化) class System_Function: {0}\n".format(self.function_or_varible)) |
|
## 定义__call__方法 |
|
def __call__(self,func,name): |
|
print("{0}: 调用 {1} 方法 \n".format(func,name)) |
|
# 初始化类对象 |
|
init_args = 'init_test' |
|
obj = System_Function(init_args) # 类实例化对象 object |
|
## 像调用函数方法那样调用 类对象系统函数 __call__() |
|
obj("obj()", "__call__()") |
|
obj.__call__("obj.__call__","__call__()") |
|
程序输出显示:
init(初始化) class System_Function: init_test |
|
obj(): 调用 __call__() 方法 |
|
obj.__call__: 调用 __call__() 方法 |
输出结果很有意思,对象无需加 . (点)就可以调用类的方法,这意味着对象本身就是一个函数对象,不过这应该是类似一种软连接的操作,将对象的点调用操作连接到对象括号,这个别名。
三、系统函数__dict__类属性查询函数
功能:
在 Python 类的内部,无论是类属性还是实例属性,都是以字典(
dict
)的形式进行存储的,其中属性名作为键,而值作为该键对应的值。
代码参考案例:
class CLanguage: |
|
a = 1 |
|
b = 2 |
|
def __init__ (self): |
|
self.name = "C语言中文网" |
|
self.add = "http://c.biancheng.net" |
|
#通过类名调用__dict__ |
|
print(CLanguage.__dict__) |
|
#通过类实例对象调用 __dict__ |
|
clangs = CLanguage() |
|
print(clangs.__dict__) |
|
程序输出显示:
{'__module__': '__main__', 'a': 1, 'b': 2, '__init__': <function CLanguage.__init__ at 0x0000022C69833E18>, '__dict__': <attribute '__dict__' of 'CLanguage' objects>, '__weakref__': <attribute '__weakref__' of 'CLanguage' objects>, '__doc__': None} |
|
{'name': 'C语言中文网', 'add': 'http://c.biancheng.net'} |
个人代码案例:
测试1
# test.py |
|
class System_Function: |
|
def __init__(self, function_or_varible): |
|
self.function_or_varible = function_or_varible |
|
print("init(初始化) class System_Function: {0}\n".format(self.function_or_varible)) |
|
## 定义__call__方法 |
|
def __call__(self,func,name): |
|
print("{0}: 调用 {1} 方法 \n".format(func,name)) |
|
# 初始化类对象 |
|
init_args = 'init_test' |
|
obj = System_Function(init_args) # 类实例化对象 object |
|
## 像调用函数方法那样调用 类对象系统函数 __call__() |
|
obj("obj()", "__call__()") |
|
obj.__call__("obj.__call__","__call__()") |
|
#通过类名调用__dict__ |
|
print("System_Function 类名调用系统函数__dict__,无需参数:\n",System_Function.__dict__) |
|
print("\n") |
|
#通过类实例对象调用 __dict__ |
|
print("类实例对象 obj 调用__dict__:\n",obj.__dict__) |
|
程序输出显示:
init(初始化) class System_Function: init_test |
|
obj(): 调用 __call__() 方法 |
|
obj.__call__: 调用 __call__() 方法 |
|
System_Function 类名调用系统函数__dict__,无需参数: |
|
{'__module__': '__main__', '__init__': <function System_Function.__init__ at 0x000001EA556B1378>, '__call__': <function System_Function.__call__ at 0x000001EA556B17B8>, '__dict__': <attribute '__dict__' of 'System_Function' objects>, '__weakref__': <attribute '__weakref__' of 'System_Function' objects>, '__doc__': None} |
|
类实例对象 obj 调用__dict__: |
|
{'function_or_varible': 'init_test'} |
测试2
# test.py |
|
class System_Function: |
|
def __init__(self,function_or_varible): |
|
self.function_or_varible = function_or_varible |
|
print("init(初始化) class System_Function: {0}\n".format(self.function_or_varible)) |
|
## 定义__call__方法 |
|
def __call__(self,func,name): |
|
self.func = func |
|
self.name = name |
|
print("{0}: 调用 {1} 方法 \n".format(self.func,self.name)) |
|
# 初始化类对象 |
|
init_args = 'init_test' |
|
obj = System_Function(init_args) # 类实例化对象 object |
|
## 像调用函数方法那样调用 类对象系统函数 __call__() |
|
obj("obj()", "__call__()") |
|
obj.__call__("obj.__call__","__call__()") |
|
#通过类名调用__dict__ |
|
print("System_Function 类名调用系统函数__dict__,无需参数:\n",System_Function.__dict__) |
|
print("\n") |
|
#通过类实例对象调用 __dict__ |
|
print("类实例对象 obj 调用__dict__:\n",obj.__dict__) |
|
程序输出显示:
init(初始化) class System_Function: init_test |
|
obj(): 调用 __call__() 方法 |
|
obj.__call__: 调用 __call__() 方法 |
|
System_Function 类名调用系统函数__dict__,无需参数: |
|
{'__module__': '__main__', '__init__': <function System_Function.__init__ at 0x0000019AF6801378>, '__call__': <function System_Function.__call__ at 0x0000019AF68017B8>, '__dict__': <attribute '__dict__' of 'System_Function' objects>, '__weakref__': <attribute '__weakref__' of 'System_Function' objects>, '__doc__': None} |
|
类实例对象 obj 调用__dict__: |
|
{'function_or_varible': 'init_test', 'func': 'obj.__call__', 'name': '__call__()'} |
需要注意的一点是,该属性可以用类名或者类的实例对象来调用,用类名直接调用
__dict__
,会输出该由类中所有类属性组成的字典(未赋予实例化的所有属性——函数名);而使用类的实例对象调用__dict__
,会输出由类中所有实例属性组成的字典(包括所有传过去的形参参数)。
对比测试1 和测试2 的输出结果,可以知道当使用类的实例化对象去调用__dict__
系统函数时,会自动调用类的self
实例,self
关键字代表了类的所有的实例,每一个类实例化都会有的一个属性
四、系统函数__str__()描述类信息函数
功能:
如果定义了该函数,当
return
信息 可用于定义当前类__str__()
方法定义的描述信息
- 参数:无
- 返回值:一般返回对于该类的描述信息
代码演示:
class Test(object): |
|
def __str__(self): |
|
return '这是关于这个类的描述' |
|
test = Test() |
|
print(test) |
运行结果:
五、系统函数__getattr__()调用类未定义之信息反馈函数
功能:
当调用的属性或者方法不存在时,会返回该方法(
__getattr__()
)定义的信息
class Test(object): |
|
def __getattr__(self, key): |
|
print('这个key:{}不存在'.format(key)) |
|
test = Test() |
|
test.a |
|
运行结果:
六、系统函数__setattr__()拦截类未定义属性及值的函数
功能:
拦截当前类中不存在的属性与值
代码演示:
class Test(object): |
|
def __setattr__(self,key,value): |
|
if key not in self.__dict__: |
|
self.__dict__[key] = value |
|
t = Test() |
|
t.name ='dewei' |
|
t.name |
|
运行结果: