I write a function (Python 3.x) for use it as a class decorator than make attributes private or public. First of all I wrote my 'private' function:
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
then I use two different methods to declare Private and Pubblic method, like this:
def setPriv(*lists):
return private(attrlist=(lambda args: args in lists))
def setPub(*lists):
return private(attrlist=(lambda args: args not in lists))
at this point I test my work:
@setPriv('name')
class t1:
def __init__(self,name,age):
self.name = name
self.age = age
But when I create my first instance
a = t1('Bob',40)
I got this error:
> 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
Thanks in advance