0

This is my python code:

class SomeClass:
    def __init__(self, apple):
        self.apple = apple

    def set_apple_to(self, new_apple):
        self.apple = new_apple

I made function 'set_apple_to' becaues I think changing member variable directly could make hard to manage code and is dangerous. Now I am wondering what the name is that prevent changing member variable directly from external user.

David Jung
  • 21
  • 4
  • Nothing prevents changing `.apple` :) – Mathias R. Jessen Feb 04 '22 at 11:58
  • https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes – lamsal Feb 04 '22 at 12:04
  • 2
    The concept of private/protected class attributes doesn't exist in Python – DarkKnight Feb 04 '22 at 12:07
  • Does this answer your question? [Does Python have “private” variables in classes?](https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes) – buran Feb 04 '22 at 12:08
  • In python there is no need for getters and setters in a case like this. There might be cases when you use property or property and setter. – buran Feb 04 '22 at 12:10
  • Although attributes can't be private (cf [this question](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name)), it can be useful to define **setters** to deal with maintaining state, or providing useful interfaces. – Lenormju Feb 04 '22 at 16:25

0 Answers0