I have a question for Python restrictors.

Asked 1 years ago, Updated 1 years ago, 115 views

class PayGildong:

    def __init__(self):
        self.day = 25
        self.__pay = 1000000

    def setPay(self, pay):
        self.__pay = pay

    def getPay(self):
        return self.__pay


gd = PayGildong()

gd.day = 14
gd.__pay = 800000

print(gd.__pay, gd.day)

When you do this, it seems that gd.__pay = 800000 should have an error, but I wonder why it is output without an error.

python-3.x

2022-09-19 23:21

1 Answers

It would be helpful if you refer to the document below.
https://docs.python.org/ko/3/tutorial/classes.html?#private-variables

A "private" instance variable that can only be accessed inside the object does not exist in Python. However, there is a convention that most Python codes follow: names that begin with an underscore (for example, _spam) should be treated as non-public parts of the API.


2022-09-19 23:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.