Idea to support public/private.

  • Thread starter Brian Allen Vanderburg II
  • Start date
B

Brian Allen Vanderburg II

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__
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__
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
 
B

Bruno Desthuilliers

Brian Allen Vanderburg II a écrit :
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.

It has already been done at least a couple times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top