2
我編寫一個函數(Python 3.x),將它用作類裝飾器,而不是使屬性成爲私有的或公共的。在這一點上我測試了我的工作我的類裝飾器中的_getattr_上的Python遞歸
def setPriv(*lists):
return private(attrlist=(lambda args: args in lists))
def setPub(*lists):
return private(attrlist=(lambda args: args not in lists))
:所有的 首先,我寫我的「私人」功能:
def private(attrlist):
def wrapper(obj):
class fPrivate:
def __init__(self,*args,**kwargs):
self.__wrapped = obj(*args,**kwargs)
def __getattr__(self,args):
if attrlist(args):
raise TypeError('Get on a provate attr')
else:
return getattr(self.__wrapped,args)
def __setattr__(self,args,val):
if args == '_fPrivate.__wrapped':
self.__dict__[args] = val
elif attrlist(args):
raise TypeError('Set on private')
else:
setattr(self.__wrapped,args,val)
return fPrivate
return wrapper
然後我用兩種不同的方法來聲明私有和Pubblic方法,如下所示:
@setPriv('name')
class t1:
def __init__(self,name,age):
self.name = name
self.age = age
但是,當我創建我的第一個實例
a = t1('Bob',40)
我得到這個錯誤:事先
> File "C:\Code\utils\private.py", line 11, in __getattr__
getattr(self.__wrapped,args)
File "C:\Code\utils\private.py", line 11, in __getattr__
getattr(self.__wrapped,args)
File "C:\Code\utils\private.py", line 11, in __getattr__
getattr(self.__wrapped,args)
File "C:\Code\utils\private.py", line 11, in __getattr__
getattr(self.__wrapped,args)
File "C:\Code\utils\private.py", line 8, in __getattr__
if attrlist(args):
File "C:\Code\utils\private.py", line 25, in <lambda>
return private(attrlist=(lambda args: args in lists))
RecursionError: maximum recursion depth exceeded in comparison
感謝
'getattr'導致'__getattr__'被調用。 – Vallentin
@Vallentin不一定。首先調用__getattribute__'。 –