How can I reach a private variable within the object

前端 未结 3 886
慢半拍i
慢半拍i 2021-01-20 10:06

I would like to modify an object private variable

class Example():
    __myTest1 = 1
    __myTest2 = 1
    def __init__(self):
        pass
    def modifyTes         


        
3条回答
  •  天涯浪人
    2021-01-20 11:01

    Try adding a single underscore and the class name to the beginning of the variable.

    def modifyTest(name = 'Test1', value):
        setattr(self, '_Example__my' + name, value)
    

提交回复
热议问题