How can I reach a private variable within the object

前端 未结 3 922
慢半拍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:00

    class Example():
        __myTest1 = 1
        __myTest2 = 1
        def __init__(self):
            pass
        def modifyTest(self, name, value):
            setattr(self, '__my'+name, value)
    

    Optional variables must come after mandatory variables.

提交回复
热议问题