Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-在派生类中重新定义类属性_Python_Inheritance - Fatal编程技术网

Python-在派生类中重新定义类属性

Python-在派生类中重新定义类属性,python,inheritance,Python,Inheritance,假设我有一个基类,其中有一个\uu标志属性,可由@classmethod访问 class Base(object): __flag = None def __init__(self) : pass @classmethod def flag(self): return self.__flag 假设我还有一个派生类,我在其中更改了属性 class Derived(Base): __flag = True 然后,当我尝试

假设我有一个基类,其中有一个
\uu标志
属性,可由
@classmethod
访问

class Base(object):
    __flag = None

    def __init__(self) :
        pass

    @classmethod
    def flag(self):
        return self.__flag
假设我还有一个派生类,我在其中更改了属性

class Derived(Base):
    __flag = True
然后,当我尝试访问派生类的属性时,我得到基类的属性:

In [3]: print Derived.flag()
None

为什么??我真的不明白。

\uu标志
中删除
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu>,它会很好地工作。像这样:

class Base(object):
    flagAtt = None

    def __init__(self) :
       pass

    @classmethod
    def flag(self):
        return self.flagAtt

class Derived(Base):
    flagAtt = True
其中:

>>> print Derived.flag()
True
>>> print Base.flag()
None

\uu标志
中删除
\uuu
,它将很好地工作。像这样:

class Base(object):
    flagAtt = None

    def __init__(self) :
       pass

    @classmethod
    def flag(self):
        return self.flagAtt

class Derived(Base):
    flagAtt = True
其中:

>>> print Derived.flag()
True
>>> print Base.flag()
None
这是因为Python中的“隐藏”变量的存储方式不同。这里有一点魔力

下面是一个示例,说明它为什么不起作用:

class Base(object):
    __flag = 'base'
    _other_flag = 'base'

    def __init__(self) :
        pass

    @classmethod
    def flag(self):
        return self.__flag

    @classmethod
    def other_flag(self):
        return self._other_flag

class Derived(Base):
    __flag = 'derived'
    _other_flag = 'derived'

print 'base flag', Base.flag()
print 'derived flag', Derived.flag()
print 'base other flag', Base.other_flag()
print 'derived other flag', Derived.other_flag()

# Note the following 2 statements:
print 'base flag property', Derived._Base__flag
print 'derived flag property', Derived._Derived__flag

print 'base other flag property', Base._other_flag
print 'derived other flag property', Derived._other_flag
正如您在底部看到的,它存储在不同的变量中,并以静默方式转换为
Base.flag
方法中的变量。

这是因为Python中的“隐藏”变量的存储方式不同。这里有一点魔力

下面是一个示例,说明它为什么不起作用:

class Base(object):
    __flag = 'base'
    _other_flag = 'base'

    def __init__(self) :
        pass

    @classmethod
    def flag(self):
        return self.__flag

    @classmethod
    def other_flag(self):
        return self._other_flag

class Derived(Base):
    __flag = 'derived'
    _other_flag = 'derived'

print 'base flag', Base.flag()
print 'derived flag', Derived.flag()
print 'base other flag', Base.other_flag()
print 'derived other flag', Derived.other_flag()

# Note the following 2 statements:
print 'base flag property', Derived._Base__flag
print 'derived flag property', Derived._Derived__flag

print 'base other flag property', Base._other_flag
print 'derived other flag property', Derived._other_flag

正如您在底部看到的,它存储在不同的变量中,并以静默方式转换为
Base.flag
方法中的变量。

Python对以两个下划线开头的变量(除了那些也以两个下划线结尾的变量)执行名称更改,因此,
基本
中的
\uu标志
\uu标志
不同。您可以使用
dir

>>> class Base(object):
...     __flag = True
... 
>>> dir(Base)
['_Base__flag', '__class__', '__delattr__', ......   # lots of others
\uuu标志
变量在此处转换为
\u Base\uu标志
。在
Derived
中,它被转换为
\u-Derived\u-flag
,因此您实际上并没有重写任何内容,只是引入了一个新变量


如果希望变量可重写,请不要使用以两个下划线开头的名称。

Python对以两个下划线开头的变量(除了也以两个下划线结尾的变量)执行名称篡改,因此
\u flag
中的
\u flag
\u flag
不同。您可以使用
dir

>>> class Base(object):
...     __flag = True
... 
>>> dir(Base)
['_Base__flag', '__class__', '__delattr__', ......   # lots of others
\uuu标志
变量在此处转换为
\u Base\uu标志
。在
Derived
中,它被转换为
\u-Derived\u-flag
,因此您实际上并没有重写任何内容,只是引入了一个新变量


如果您希望变量是可重写的,请不要使用以两个下划线开头的名称。

哦,就这么简单!非常感谢你!哦,就这么简单!非常感谢你!