Alg Problem (new to Python)

Steinar Knutsen sk at fyemf.fys.ntnu.no
Sun Jun 20 16:47:58 EDT 1999


In article <7k38gf$khr$1 at holly.prod.itd.earthlink.net>, Steve Hunter
<Steve at HOSYS.com> wrote:

> The out put of a program is below....the problem is the tasks are
>being added to every project vs. to the specific project....it sees
>like an alg program but I can't see it....brand new to Python...have
>mercy.
>
> [...]
>
>the program below is in on .py file
>
>
>import sys
>
>projectDict = {} #MAPS PROJECT id string to PROJECT  object
>taskDict = {}  #MAPS TASK id string to TASK object
>
>class Project:
> _id = ''
> _name = ''
> _descr = ''
> _tasks = {}    #list of Tasks I contain

The dictionary Project._tasks is a mutable object, so when your method
for adding a task adds a new task to the dictionary, it adds the task to
the class attribute (Instead of making a new object as with strings and
integers, for instance.). Putting something like
   self.tasks = {}
into the __init__-method of the class Project will solve your problem.
Don't worry, the behaviour of mutable versus immutable objects is one of
the few things that I think often bites people new to Python.
-- 
Steinar




More information about the Python-list mailing list