0

I'm having an issue where it seems that information in a list is not "reseting" to defaults when i believe it should be. Please help me understand what is going on

I have 2 files, the first contains the following code.

File: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)

And the second File is as follows:

File: 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")
#

And what I'm not getting is I would expect to see the functions "actualthing" and "expectedthing" to yield the same results, however they don't.

This is what I get as a result

>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

Clearly this isn't the actual code I'm using, however it yields the same result. Due to the way I have written this script the "other_thing" may change based on the options and arguments provided by the user during script execution, So I can't just say make it equal to this. It is a list because I need to be able to change in length, anywhere from 1 item to 40 items(once again based on the user input). Any idea on how I should handle this?

Thanks for help, anything is useful.

Bach
  • 6,145
  • 7
  • 36
  • 61
Linx
  • 156
  • 2
  • Also, `self.needed_input=needed_input` and `self.other_thing=other_thing` don't copy the list. – thefourtheye Feb 21 '14 at 11:21
  • Also please have a look at [this answer](http://stackoverflow.com/a/20019569/1903116) to know what is actually happening and how to fix this – thefourtheye Feb 21 '14 at 11:27
  • Two things, 1) we should copy the list only as I have shown in the answer I have linked in the comment. 2) Don't keep mutable objects in the parameters, check the dup question. – thefourtheye Feb 21 '14 at 11:43
  • @thefourtheye Thank you very much, I didn't see this when I was searching. Thank you. – Linx Feb 21 '14 at 11:45

0 Answers0