__init__列表在删除后保持更改

时间:2014-02-21 11:13:56

标签: python list default init traceback

我遇到的问题是,当我相信它应该是列表中的信息似乎没有“重置”到默认值。请帮助我了解正在发生的事情

我有2个文件,第一个包含以下代码。

文件:testing.py

import sys
import getopt
import time

import testing2

entry = ["one","two"]
for input in entry:
    obj = testing2.thing(input)
    print ("{0}:\n  Input:{1}\n  Other:{2}").format(1,obj.needed_input,obj.other_thing)
    obj.change()
    print ("{0}:\n  Input:{1}\n  Other:{2}").format(2,obj.needed_input,obj.other_thing)
print ("\n")

for input in entry:
    obj = testing2.expectedthing(input)
    print ("{0}:\n  Input:{1}\n  Other:{2}").format(1,obj.needed_input,obj.other_thing)
    obj.change()
    print ("{0}:\n  Input:{1}\n  Other:{2}").format(2,obj.needed_input,obj.other_thing)

print("\n")
for input in entry:
    obj = testing2.actualthing(input)
    print ("{0}:\n  Input:{1}\n  Other:{2}").format(1,obj.needed_input,obj.other_thing)
    obj.change()
    print ("{0}:\n  Input:{1}\n  Other:{2}").format(2,obj.needed_input,obj.other_thing)

sys.exit(1)

第二个文件如下:

文件:testing2.py

def error_die(errstring):
    print('A Fatal Error has occurred: "{0}"').format(errstring)
    sys.exit(0)

class thing:
    def __init__(self,needed_input,other_thing="something"):
        if needed_input == None:
            error_die('invalid input')
        self.needed_input=needed_input
        self.other_thing=other_thing
    def change(self):
        self.other_thing="something else"
#

class expectedthing:
    def __init__(self,needed_input,other_thing=["one thing","two thing"]):
        if needed_input == None:
            error_die('invalid input')
        self.needed_input=needed_input
        self.other_thing=other_thing
    def change(self):
        self.other_thing=["one thing","three thing"]
#

class actualthing:
    def __init__(self,needed_input,other_thing=["one thing","two thing"]):
        if needed_input == None:
            error_die('invalid input')
        self.needed_input=needed_input
        self.other_thing=other_thing
    def change(self):
        self.other_thing.append("three thing")
        self.other_thing.remove("two thing")
#

而我没有得到的是我希望看到函数“realthing”和“expectthing”产生相同的结果,但是他们没有。

这就是我得到的结果

>python testing.py

1:
  Input:one
  Other:something
2:
  Input:one
  Other:something else
1:
  Input:two
  Other:something
2:
  Input:two
  Other:something else


1:
  Input:one
  Other:['one thing', 'two thing']
2:
  Input:one
  Other:['one thing', 'three thing']
1:
  Input:two
  Other:['one thing', 'two thing']
2:
  Input:two
  Other:['one thing', 'three thing']


1:
  Input:one
  Other:['one thing', 'two thing']
2:
  Input:one
  Other:['one thing', 'three thing']
1:
  Input:two
  Other:['one thing', 'three thing']
Traceback (most recent call last):
  File "testing.py", line 45, in <module>
    obj.change()
  File "Z:\scripts\testing2.py", line 25, in change
    self.other_thing.remove("two thing")
ValueError: list.remove(x): x not in list

显然,这不是我正在使用的实际代码,但它会产生相同的结果。由于我编写此脚本的方式,“other_thing”可能会根据用户在脚本执行期间提供的选项和参数而更改,因此我不能只说让它等于此。这是一个列表,因为我需要能够改变长度,从1项到40项(再次根据用户输入)。我对如何处理这个有任何想法吗?

感谢您的帮助,一切都很有用。

0 个答案:

没有答案