Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Idea to support public/private.

Reply
Thread Tools

Idea to support public/private.

 
 
Brian Allen Vanderburg II
Guest
Posts: n/a
 
      01-22-2009
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

 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      01-23-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: App idea, Any idea on implementation? Matthew_WARREN@bnpparibas.com Python 0 02-05-2008 05:50 PM
App idea, Any idea on implementation? Dr Mephesto Python 3 02-05-2008 06:55 AM
Nested conditional expressions ---- good idea/bad idea? nimmi_srivastav@yahoo.com C Programming 10 02-02-2005 10:51 PM
ANN: SCons.0.96 adds Fortran 90/95 support, better Qt support,platform-independent file system actions, improved debugging, lots more Steven Knight Python 0 08-18-2004 03:57 PM
Getting Third Party Component Suppliers to support NUnit and NUnitASP to support test driven development in web pages Nick Zdunic ASP .Net 0 11-05-2003 10:45 AM



Advertisments