List & Object Duplication Issues In Python
Solution 1:
I know this isn't exactly the answer the OP was looking originally looking for, but it might be the correct answer, and if the OP manages to find the issue, then maybe they will think it is the correct answer too.
Try to debug the code with breakpoints. For ease of use nothing beats IPython pdb, although pdb - Python debugger, and winpdb are also useful. If you are using Spyder or PyDev plugin for eclipse debugging is built into the graphical user interface - just set your breakpoints and go.
For your code:
- Install Ipdb and its dependencies (IPython, &c.)
From the system command line you can use the handy
ipdb
script.~ $ ipdb --help usage: ipdb.py scriptfile [arg] ... ~ $ ipdb species.py args > ~\species.py(1)<module>() ---> 1import random 2from pprint import pprint 3 ipdb>
Commands are the same as
pdb
and for any debugger. Type help after theipdb>
prompt for a list of commands.The basic commands are
n
for next to execute the current line and step to the next,s
or step to execute the next line of a called object, such as a function or class constructor or method,r
to return to the caller,b
to set abreakpoint
c
to continue execution until the next breakpoint andq
to quit or exit
Get more help by typing
help <cmd>
. EGipdb> help r r(eturn) Continue execution until the current function returns.
Set a breakpoint in your code where you think the trouble might be and step through it.
ipdb> b67 Breakpoint 1 at ~\species.py:67
You can use Python commands and examine variables with few restrictions -
retval
,rv
and any of theipdb
commands will return the result of thatipdb
call - so usevars()['<varname>']
instead.List comprehensions in
ipdb
are like for-loops, and son
will step through the list comprehension as many times as the length of the iterable.Hit enter/return key to repeat the last
ipdb
command. EGipdb> n> ~\species.py(67)<module>() 66 1--> 67 while len([y for y in petri_dish if y.status == 1]) > 1: 68 turn += 1 ipdb> > ~\species.py(67)<module>() 66 1--> 67 while len([y for y in petri_dish if y.status == 1]) > 1: 68 turn += 1 ipdb> > ~\species.py(69)<module>() 68 turn += 1 ---> 69 move_around() 70 ipdb> turn 2
Step into a function to see what it does
ipdb> s --Call-- > ~\species.py(60)move_around() 59 ---> 60 def move_around(): 61 for x in list(petri_dish):
Hopefully you get the idea. Learning to use a debugger is going to have more payback than having someone else spot your bug. At the very least, if you can pinpoint where the extra duplicates start appear, then you can ask a more specific question and you will get better answers.
Happy coding!
Post a Comment for "List & Object Duplication Issues In Python"