Base class 变量不存储由 Derived class 设置的值
Base class variable does not store value which is set by Derived class
我是 python 的新手。
基础 class/python 文件(Base.py).
SESSION_ID = ""
def m1():
print "SESSION_ID in base: ",SESSION_ID
派生 class(Derived.py)
from Base import *
class Derived():
def m2(self):
global SESSION_ID
SESSION_ID = 10
print "SESSION_ID in derived: ", SESSION_ID
def main():
c2 = Derived()
c2.m2()
m1()
if __name__ == "__main__":
main()
当我执行下面的 Derived.py 文件时,输出是:
SESSION_ID in derived: 10
SESSION_ID in base:
我希望在m2() 中设置的值反映在m1() 中。所以预期的输出是:
SESSION_ID in derived: 10
SESSION_ID in base: 10
你能帮忙吗?
模块中的全局变量只是一个属性(即一个成员
该模块的实体)。
因此,当您使用 import *
时,会创建新的本地模块全局 SESSION_ID
,因此基础中的 SESSION_ID 不受您在派生。
基本上,修改base.SESSION_ID
不需要在Derived.py
中使用global
语句,调整import
就可以了,看下面的代码:
from settings import base
class Derived():
def m2(self):
base.SESSION_ID = 10
print "SESSION_ID in derived: ", base.SESSION_ID
def main():
c2 = Derived()
c2.m2()
base.m1()
if __name__ == "__main__":
main()
您的 Derived
class 不是 来自 Base.py
中的任何内容。在这里,您只是从 Derived
中调用 Base
的基本函数,仅此而已。
这是 Python3 中 class inheritance 的示例:
>>> class Base():
>>> SESSION = 42
>>>
>>> def print_session(self):
>>> print("Base session : %d" % self.SESSION)
>>>
>>> class Derived(Base):
>>> SESSION = 999
>>>
>>> d = Derived()
>>> d.print_session()
Base session : 999
我会尽可能避免使用全局和 class 范围的变量。这些会使您的程序更难理解(如果其他东西改变了您下面的全局,则很难注意到)和测试(您需要在每次测试之间重置所有全局状态;创建新的空状态通常更容易)。
我可能会通过创建一个状态对象来重构这个例子:
class State:
def __init__(self):
self.session_id = ''
然后使它明确地成为 属性,比如说,基数 class:
class Base:
def __init__(self, state):
self.state = state
def m1(self):
print("SESSION_ID in base: " + str(self.state.session_id))
class Derived(Base):
def m2(self):
self.state.session_id = '10'
print("SESSION_ID in derived: " + str(self.state.session_id))
然后在你的主函数中你需要显式地创建状态对象并传入它
def main():
state = State()
c2 = Derived(state)
c2.m2()
c2.m1()
但是,至关重要的是,您的测试无需担心状态泄漏
def test_m2():
state = State()
obj = Derived(state)
obj.m2()
assert state.session_id == '10'
def test_m1():
state = State()
obj = Base(state)
obj.m1()
# If the session ID was a global or a class variable,
# you'd get a different result if m2() was called or not
assert state.session_id == ''
我是 python 的新手。 基础 class/python 文件(Base.py).
SESSION_ID = ""
def m1():
print "SESSION_ID in base: ",SESSION_ID
派生 class(Derived.py)
from Base import *
class Derived():
def m2(self):
global SESSION_ID
SESSION_ID = 10
print "SESSION_ID in derived: ", SESSION_ID
def main():
c2 = Derived()
c2.m2()
m1()
if __name__ == "__main__":
main()
当我执行下面的 Derived.py 文件时,输出是:
SESSION_ID in derived: 10
SESSION_ID in base:
我希望在m2() 中设置的值反映在m1() 中。所以预期的输出是:
SESSION_ID in derived: 10
SESSION_ID in base: 10
你能帮忙吗?
模块中的全局变量只是一个属性(即一个成员 该模块的实体)。
因此,当您使用 import *
时,会创建新的本地模块全局 SESSION_ID
,因此基础中的 SESSION_ID 不受您在派生。
基本上,修改base.SESSION_ID
不需要在Derived.py
中使用global
语句,调整import
就可以了,看下面的代码:
from settings import base
class Derived():
def m2(self):
base.SESSION_ID = 10
print "SESSION_ID in derived: ", base.SESSION_ID
def main():
c2 = Derived()
c2.m2()
base.m1()
if __name__ == "__main__":
main()
您的 Derived
class 不是 来自 Base.py
中的任何内容。在这里,您只是从 Derived
中调用 Base
的基本函数,仅此而已。
这是 Python3 中 class inheritance 的示例:
>>> class Base():
>>> SESSION = 42
>>>
>>> def print_session(self):
>>> print("Base session : %d" % self.SESSION)
>>>
>>> class Derived(Base):
>>> SESSION = 999
>>>
>>> d = Derived()
>>> d.print_session()
Base session : 999
我会尽可能避免使用全局和 class 范围的变量。这些会使您的程序更难理解(如果其他东西改变了您下面的全局,则很难注意到)和测试(您需要在每次测试之间重置所有全局状态;创建新的空状态通常更容易)。
我可能会通过创建一个状态对象来重构这个例子:
class State:
def __init__(self):
self.session_id = ''
然后使它明确地成为 属性,比如说,基数 class:
class Base:
def __init__(self, state):
self.state = state
def m1(self):
print("SESSION_ID in base: " + str(self.state.session_id))
class Derived(Base):
def m2(self):
self.state.session_id = '10'
print("SESSION_ID in derived: " + str(self.state.session_id))
然后在你的主函数中你需要显式地创建状态对象并传入它
def main():
state = State()
c2 = Derived(state)
c2.m2()
c2.m1()
但是,至关重要的是,您的测试无需担心状态泄漏
def test_m2():
state = State()
obj = Derived(state)
obj.m2()
assert state.session_id == '10'
def test_m1():
state = State()
obj = Base(state)
obj.m1()
# If the session ID was a global or a class variable,
# you'd get a different result if m2() was called or not
assert state.session_id == ''