class and private variables

后端 未结 3 1840
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 04:03
class Test1:
    def __init__( self ):
        self.__test = 1
    def getvalue( self ):
        return self.__test

class Test2( Test1 ):
    def __init__( self ):
        T         


        
3条回答
  •  青春惊慌失措
    2021-01-27 04:22

    This behaviour is due to the name mangling for attribute names starting with __. Basically, __test gets mangled to _Test1__test inside Test1 and to _Test2__test inside Test2, so they are two different attributes.

提交回复
热议问题