1

I have the following Python-Code. I just can't get the instance variable to be read only. Help appreciated.

class Parrot(object):

    def __init__(self):
        self._voltage = '100000'

    @property
    def voltage(self):
        """Get the current voltage."""
        return self._voltage

a = Parrot()

print(a._voltage)

a._voltage = '500000'

print(a._voltage)

Edit:

The point of this question was to understand, how a property could replace the old variable. Somehow everybody just points out that Python is about being mature, and that it is our responsibility not to use "private" variables, since they are visible in python. But nobody pointed out that you'd just turn the old variable in this case

voltage

private

_voltage

and replace the old variable (voltage) with the property

@property
def voltage(self):

which would leave the way you access attributes in this class the same, so that nobody who uses this class has to change their code.

-- like the way you access variables, since you can still access the property like a variable -- e.g.:

a.voltage = 'over 9000'

But it gives more control to the developers of this class (turn voltage to read only). I just felt that nobody did actually explain the mechanics of properties in an understandable way... -> I was not able to understand properties although I googled first. Anyways... kinda ridiculous, since it does not seem to pose any kind of difficulties now.

Cheers
Nimi

Nima Mousavi
  • 1,601
  • 2
  • 21
  • 30
  • 4
    I don't see anything in your code that designates `_voltage` as read-only. If you want read-only semantics, you have to use the **property**, not the backing variable. – Robert Harvey Aug 26 '14 at 16:49
  • I think you should take a look at this question: https://stackoverflow.com/questions/14594120/python-read-only-property – Uxío Aug 26 '14 at 16:54
  • There's no way to do that in Python. Python is for consenting adults only. If you don't trust someone not to access your private attribute, don't give them your object. – Adam Smith Aug 26 '14 at 17:02
  • As a complement of the other comments, the property here is `voltage` not `_voltage`. – Sylvain Leroux Aug 26 '14 at 17:07
  • Thx for the Comment. I didn't expect a backing variable to be their. I think I get the difference now. – Nima Mousavi Aug 27 '14 at 00:33

1 Answers1

2

That's normal, since Python isn't a bondage & discipline language. There's no real equivalent to 'private', but sometimes attributes with two leading underscores are used, though these are inteded to avoid problems when inherating. The one leading underscore is just to avoid importing this when using a from xx import * import.

TidB
  • 1,749
  • 1
  • 12
  • 14
  • The documentation states that it is possible to create a read-only variable using the property decorator. https://docs.python.org/3/library/functions.html#property I was just trying to reproduce the results. There's no harm in having a read-only variable. – Nima Mousavi Aug 27 '14 at 00:24