File: class/Extras/Code/Misc/privates.py

File: class/Extras/Code/Misc/privates.py

"""
this is a partial solution: need to augment to allow
subclasses to set private self attributes, and use
__getattr__ and wrapper/proxy class for attr fetches
"""
class PrivateExc(Exception): pass
class Privacy:
 def __setattr__(self, attrname, value): # on self.attrname = value
 if attrname in self.privates:
 raise PrivateExc(attrname, self)
 else:
 self.__dict__[attrname] = value # self.attrname = value loops!
class Test1(Privacy):
 privates = ['age']
class Test2(Privacy):
 privates = ['name', 'pay']
 def __init__(self):
 self.__dict__['name'] = 'Tom'
x = Test1()
y = Test2()
x.name = 'Bob'
#y.name = 'Sue' # <== fails
y.age = 30
x.age = 40 # <== fails



AltStyle によって変換されたページ (->オリジナル) /