0
class OurAtt():
    def __init__(self):
        self.Log = False

    def setLog(self):
        self.Log = True

    def clearLog(self):
        self.Log = False

class OurClass(object):

    def __init__(self):
        self.__OurAtt = OurAtt()

    @property
    def OurAtt(self):
        return self.__OurAtt

    @OurAtt.setter
    def OurAtt(self, val):
       raise Exception("can't modify the attribute" )

x = OurClass()
x.OurAtt.setLog()
print x.OurAtt.Log  # print True
x.OurAtt.Log = False
print x.OurAtt.Log  # sets to False Aim set this through function call     x.OurAtt.setLog()  I want to restrict the access, something like private variable.

Final aim is Log should be the attribute of OurAttr and should be protected by getter and setters or properties. Its like nesting of properties. and hierarchy should be maintained like object.OurAttr.Log

I researched and got the following link.

Python: multiple properties, one setter/getter

But It is not hitting my aim.

I am actually new to getter, setter and properties. Thanks in advance

Community
  • 1
  • 1
  • So what is actually wrong with your code? Also you seem to be using python 2, therefore your classes must be new style classes for this to work. Your classes should inherit from `object`. – Paul Rooney Jan 27 '16 at 06:14
  • Thanks Rooney, Just added the changes. What might be solution to this? –  Jan 27 '16 at 06:23
  • I'm not clear on your question. In what way does the code you currently have (which has been altered by an editor) not work as expected? – Paul Rooney Jan 27 '16 at 06:25
  • Edited the post. Aim is to set self.Log through function call x.OurAtt.setLog() I want to restrict the access, something like private variable. –  Jan 27 '16 at 06:34
  • Why did you think that doing it this way would help you reach your goal? – Ignacio Vazquez-Abrams Jan 27 '16 at 07:13

1 Answers1

1

I believe you are over-complicating the issue. If you want to prevent access to the attributes of OurAtt, the @property decorator should be used withing OurAtt. Instances of the OurAtt class will implement this protected-access behavior always, including when they are members of OurClass. You don't need to do anything with the @property decorator in OurClass unless you want to prevent modifying members of that class.

This, I think, does what you are trying to accomplish. It runs under 2.7 - if you are using an earlier version your mileage may vary.

class OurAttr(object):
    def __init__(self):
        self._log = False

    @property
    def log(self):
        return self._log

    @log.setter
    def log(self, value):
        raise AttributeError("Cannot set 'log' attribute directly.")

    @log.deleter
    def log(self):
        raise AttributeError("Cannot delete 'log' attribute directly.")

    def setLog(self):
        self._log = True
        print "Log is", self._log

    def clearLog(self):
        self._log = False
        print "Log is", self._log

class OurClass(object):
    def __init__(self):
        self.OurAttr = OurAttr()

oc = OurClass()
oc.OurAttr.setLog()
oc.OurAttr.clearLog()
oc.OurAttr.log = False   # Raises exception

Output is:

$ python2.7 test.py
Log is True
Log is False
Traceback (most recent call last):
  File "test.py", line 33, in <module>
    oc.OurAttr.log = False
  File "test.py", line 11, in log
    raise AttributeError("Cannot set 'log' attribute directly.")
AttributeError: Cannot set 'log' attribute directly.
aghast
  • 14,785
  • 3
  • 24
  • 56
  • Thanks Austin, class OurClass(): # inheritance from object is removed. def __init__(self): self.OurAttr = OurAttr() oc = OurClass() oc.OurAttr.setLog() oc.OurAttr.clearLog() oc.OurAttr.log = False # Not raising exception , What happens if I remove object inheritance? –  Jan 27 '16 at 10:35
  • I have no idea, and a few minutes of searching around didn't enlighten me. You'll have to try it and see if you can diagnose what's happening. – aghast Jan 27 '16 at 21:59
  • If we don't inherited from Object, It will be old style class and decorators are not supported. That's why this behaviour. –  Jan 28 '16 at 04:30