Okay so I don't really care about public/private but I was watching the lists (Does python follow its idea of readability or something like that) and I thought of a 'possible' way to add this support to the language.

I have implemented a class which allows creating both a private as well as a protected member, only it is currently a bit of work. It could perhaps be reworked into decorators.


import sys
import inspect

def get_private_codes(class_):
   codes = []
   for i in class_.__dict__:
       value = class_.__dict__[i]
       if inspect.isfunction(value):
           codes.append(value.func_code)
   return codes

def get_protected_codes(class_, codes=None):
   if codes is None:
       codes = []

   for i in class_.__bases__:
       get_protected_codes(i, codes)

   for i in class_.__dict__:
       value = class_.__dict__[i]
       if inspect.isfunction(value):
           codes.append(value.func_code)
   return codes


class Test(object):
   def __init__(self):
       self.protected = 45
       self.private = 34


   def setprotected(self, value):
       frame = sys._getframe(1)
       if frame.f_code in get_protected_codes(self.__class__):
           self.__protect_value_ZR20 = value
       else:
           raise "Protected Write Error"

   def getprotected(self):
       frame = sys._getframe(1)
       if frame.f_code in get_protected_codes(self.__class__):
           return self.__protect_value_ZR20
       else:
           raise "Protected Read Error"

   protected = property(getprotected, setprotected)

   def setprivate(self, value):
       frame = sys._getframe(1)
       if frame.f_code in get_private_codes(self.__class__):
           self.__private_value_ZR20 = value
       else:
           raise "Private Write Error"

   def getprivate(self):
       frame = sys._getframe(1)
       if frame.f_code in get_private_codes(self.__class__):
           return self.__private_value_ZR20
       else:
           raise "Private Read Error"

   private = property(getprivate, setprivate)

class Test2(Test):
   def __init__(self):
       self.protected = 1

a=Test()
b=Test2()
#print a.private
#a.private = 1
#print a.protected
#a.protected = 1

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to