首先来看看上述三个魔法方法的定义吧:
(1)__getattr__(self, item):
在访问对象的item属性的时候,如果对象并没有这个相应的属性,方法,那么将会调用这个方法来处理。。。这里要注意的时,假如一个对象叫fjs, 他有一个属性:fjs.name = "fjs",那么在访问fjs.name的时候因为当前对象有这个属性,那么将不会调用__getattr__()方法,而是直接返回了拥有的name属性了
(2)__setattr__(self, item, value):
当试图对象的item特性赋值的时候将会被调用。。
(3)__getattribute__(self, item):
这个只有在新式类中才有的,对于对象的所有特性的访问,都将会调用这个方法来处理。。。可以理解为在__getattr__之前
了解新式类与经典类见:https://blog.csdn.net/u010066807/article/details/46896835
问题:
在编程的时候使用__getattr__ , 如果访问对象item属性的时候,会发现@property的方法属性也会进入到__getattr__中,打断点进行跟踪,也不太好跟踪。最好查看这里可能怀疑与新式类有关系,果然
在@propterty修改的内容中有新式类的实例对象,最后导致。将新式类修改为经典类后,问题得到解决。后面有时间还得详细研究一下。
class FCell:
"""
定义假小区类
主要用途:适配各种增加邻区关系时使用
"""
def __init__(self, RatType, selfName):
"""
:param RatType: 定义假小区的类型['G', 'W', 'L', 'NR']
"""
self.selfName = selfName
self.RAT = RatType
self._eNB = None
self._gNB = None
self._GBSC = None
self._WNRC = None
Class B(object):
.....
def __getattr__(self, item):
print item
@property
def FCells(self):
if not self._FCells:
realFCellNum = 2
anTypeNum = 4
FakeCellBase = 5
self._FCells = [None] * anTypeNum * FakeCellBase
for index, antype in enumerate(['NR', 'L', 'W', 'G']):
for cellnum in range(0, realFCellNum):
self._FCells[FakeCellBase * index + cellnum] = FCell(antype, 'Fake' + antype + 'Cell' + str(cellnum))
print "self._FCells==", self._FCells
return self._FCells
[2019-03-29 15:55:16,358]Fail Step
[2019-03-29 15:55:16,358] File "E:\Project\ApusAT\Tcs\DEMO\TC_DEMO_AW_WMN_TEST.py", line 100, in TC_DEMO_AW_WMN_TEST
LogError(self.wmn.FCells)
File "E:\Project\ApusAT\Apus\ApsNE\WMN\wmn.py", line 887, in __getattr__
deal = '1. 请检查共享环境转换索引文件{0}中,已经配置了小区索引{1}。'.format(self.self.TestCaseNameCfgFile, item)
File "E:\Project\ApusAT\Apus\ApsNE\WMN\wmn.py", line 887, in __getattr__
deal = '1. 请检查共享环境转换索引文件{0}中,已经配置了小区索引{1}。'.format(self.self.TestCaseNameCfgFile, item)
File "E:\Project\ApusAT\Apus\ApsNE\WMN\wmn.py", line 887, in __getattr__
deal = '1. 请检查共享环境转换索引文件{0}中,已经配置了小区索引{1}。'.format(self.self.TestCaseNameCfgFile, item)
File "E:\Project\ApusAT\Apus\ApsNE\WMN\wmn.py", line 887, in __getattr__
deal = '1. 请检查共享环境转换索引文件{0}中,已经配置了小区索引{1}。'.format(self.self.TestCaseNameCfgFile, item)
。。。。。。
File "E:\Project\ApusAT\Apus\ApsNE\WMN\wmn.py", line 822, in __getattr__
if self.shareNetwork and self.isExistTestCaseNameCfgFile:
RuntimeError: maximum recursion depth exceeded while calling a Python object
[2019-03-29 15:55:16,861]
===================用例名:TC_DEMO_AW_WMN_TEST 用例结果:Tcs Error 执行时间:14 秒===================
===================开始时间:2019-03-29 15:55:02.493000 结束时间:2019-03-29 15:55:16.861000===================