Idea to support public/private.

Discussion in 'Python' started by Brian Allen Vanderburg II, Jan 22, 2009.

  1. 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
     
    Brian Allen Vanderburg II, Jan 22, 2009
    #1
    1. Advertising

  2. 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.
     
    Bruno Desthuilliers, Jan 23, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,439
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    636
    SpamProof
    Oct 21, 2003
  3. qazmlp
    Replies:
    19
    Views:
    826
    Daniel T.
    Feb 4, 2004
  4. DaveLessnau
    Replies:
    3
    Views:
    448
    Howard
    May 16, 2005
  5. Steve Holden
    Replies:
    2
    Views:
    334
    Alex Martelli
    Oct 8, 2005
Loading...

Share This Page