0

I read tons of documents and answers and so far I haven't seen anything that is protecting python class attributes from being modified by clients.

This is one example:

class Dog:
    def __init__(self):
        self._legs = 4
    @property
    def legs(self):
        return self._legs

    @legs.setter
    def legs(self, n):
        self._legs = 4

No matter we have legs.setter method or not, users can always read or modify the Dog's attribute by _legs internal names. And they can even modify it's value by calling:

dog = Dog()
dog._legs = 3

print(dog.legs)
# output 3

I don't really see how it protected things from being stolen or modified from client side?

martineau
  • 119,623
  • 25
  • 170
  • 301
jtcloud
  • 521
  • 1
  • 4
  • 18
  • 2
    You may wish to reference [this thread](https://stackoverflow.com/questions/4555932/public-or-private-attribute-in-python-what-is-the-best-way) which provides background on why this is the case in Python. – metatoaster Apr 20 '20 at 00:38
  • 1
    In all the reading you've done, did you happen upon the [first sentence here](https://docs.python.org/3/tutorial/classes.html#private-variables) – Mark Apr 20 '20 at 00:47
  • "“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python." @MarkMeyer, thanks for the link. – jtcloud Apr 20 '20 at 00:58
  • 1
    Python makes no attempt to secure parts of the program against attack from other parts of the program. – user2357112 Apr 20 '20 at 00:58
  • @metatoaster, thanks for sharing. That discussion is helpful. – jtcloud Apr 20 '20 at 01:00
  • @user2357112supportsMonica, sad to realize that. Can imagine lot of hack around when calling a server program. – jtcloud Apr 20 '20 at 01:01
  • 1
    @TonyYun: Attempting to secure the program against itself just doesn't make sense in most threat models, and even in threat models where it does make sense, `private` is woefully insufficient for that. – user2357112 Apr 20 '20 at 01:07
  • makes sense. Thanks @user2357112supportsMonica – jtcloud Apr 20 '20 at 01:16

0 Answers0