0

I'm sure this is stupidly simple, but I am trying to add words to two different dictionary files I have created.

I think my problem is here:

myDict = Dictionary.Dictionary()
myDict2 = Dictionary.Dictionary()

Because when I write out the files, it writes the same thing to both files, even though I want them to be slightly different:

So, basically, is there a way to make these separate objects, rather than pointing to the same one? If you need more of the code, I can provide that as well.

3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by jgritty
0

Here is what the dictionary class looks like:

#!/usr/local/bin/python

import sys, string

class Dictionary (object):
	def __init__(self, dictionary=[]):	

		self.dictionary=dictionary
		
	def read_dict_file(self, filename):
		try:
			dictfile = open (filename)
		except:
			print "Failed to open dictionary file: " + filename
			sys.exit(1)
			
		for line in dictfile.readlines():
			self.dictionary.append (string.strip(line))
		
		dictfile.close()
		
		
		
	def add_word (self, word):
		stripped_word = string.strip(word)
		if stripped_word in self.dictionary:
			print stripped_word + " is already in the dictionary"
		else:
			self.dictionary.append (stripped_word)
			
		
	def remove_word (self, word):
		try:
			self.dictionary.remove (string.strip(word))
		except:
			print "Warning: Could not remove "+string.strip(word)+" from dictionary"
		
	def write_dict_file(self, filename):
		try:
			dictfile = open (filename, 'w')
		except:
			print "Failed to open dictionary file for writing: " + filename
			sys.exit(1)
		
		for word in self.dictionary:
			dictfile.write (word+"\n")
		
		dictfile.close()
	
	def is_a_word (self, word):
		stripped_word = string.strip(word)
		if stripped_word in self.dictionary:
			return True
		else:
			return False

So, in the main program, I make two dictionaries. Then, I take user input, and depending on the word I add it to either one or the other dictionary, or both.

However, when I do the write at the end of the main program, it writes the same dictionary to both files. Here is the basics of the main program:

# add.py

import Dictionary, string

myDict = Dictionary.Dictionary()
myDict2 = Dictionary.Dictionary()

dict3to8 = "3to8.txt"
dict2to10 = "2to10.txt"

myDict.read_dict_file(dict3to8)
myDict2.read_dict_file(dict2to10)


s = ""
while s != "end":
	s = raw_input('>')
	if s == "end":
		break
	if s == "write":
		myDict.write_dict_file(dict3to8)
		myDict2.write_dict_file(dict2to10)
	else:
		if len(string.strip(s)) > 2 and len(string.strip(s)) < 9:
			myDict.add_word(s)
		if len(string.strip(s)) > 1 and len(string.strip(s)) < 11:
			myDict2.add_word(s)


myDict2.write_dict_file(dict2to10)
myDict.write_dict_file(dict3to8)

So, when I look at the files at the end, they both contain every word I added from length 2 to 10. Even though myDict should only be adding 3 to 8 length words.

0

Ah. Well, now I see your problem, and I'll look into figuring this out.

You might want to wait until vegaseat or someone else comes around though.

0

Looks like your problem is in line 7, you are using a list as default argument.

The addresses of a function's default arguments get set at compile time. When a mutable object like a list is used as a default argument it will have this fixed address, adding more elements to the list will change the list (hence mutable object), but not the address, so the result is a list that accumulates its contents.

Let me illustrate this ...

def myfunc(count=[]):
    count.append(7)
    return count

print myfunc()  # [7]
print myfunc()  # [7, 7]
print myfunc()  # [7, 7, 7]

This prevents this sometimes unwanted accumulation ...

def myfunc(count=None):
    if count == None:
        count = []
    count.append(7)
    return count

print myfunc()  # [7]
print myfunc()  # [7]
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.