|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problem with referencing
I am trying to get an assignment done by tonight, and I am having tons of problems due to one problem which I cannot solve. It is a reference problem...if you need the code let me know, but here is a good example. O and this only happens in the methods I made, but not if I do something like this straight through a command prompt.
OK here is the example: def example(i) j = i j = j + 2 The problem occurs because whenever I do something like this( its really an insert into a list and a push into a list), i and j remain referenced, so any changes I make to either, cause a change in both. I have never encountered something like this in any other language and am curious if you could help |
|
#2
|
||||
|
||||
|
This is because lists, dictionaries and tuples are passed by reference. You want to make a copy of a list, you do something like this:
Code:
def foo(i):
j = i[:] # Now j is a copy of i's contents
j.append(3)
P.S. What's with the handle? ![]()
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month |
|
#3
|
|||
|
|||
|
Thanks
Thank you a million times. I disagree with that aspect of having to do something like that, but thank you so much. The name is because I used to play Everquest and that was my character's name. Id explain more, but I have a deadline to meet now
|
|
#4
|
|||
|
|||
|
Still more probs
>>> board = [0, 1, 5, 4, 3, 2, 7, 8, 6, []]
>>> OrigBoard = board[:] >>> board[9].append('R') OK why is the OrigBoard affected now, is it because of the array inside the array? I got the one prob working and now I run into this heh. Just so you know, I am making an 8 puzzle solver and I want to add which way I moved the blocks. However, it ends up being every move I made because I cannot reset to this origboard when I need to. I hope you dont think Im trying to get you to do my hw, it's just that I really tried to solve these probs and am stumped and only have 4 housr left ahhhh I have figured it has to do only with that other array and am working on the solution, but any help to help me get higher than a 50% will be greatly appreciated...as this isnt even the hardest part heh (I have to implement a* and greedy searches) |
|
#5
|
||||
|
||||
|
That's cuz copies of objects are shallow copies. See note 2 in this page: http://docs.python.org/lib/typesseq.html
Now that you know what's causing it, here's how you solve it. What you need to do in such a situation, is to create a deepcopy of the original. This link should show you exactly how to do it: http://www.python.org/doc/current/lib/module-copy.html Happy hacking! |
|
#6
|
|||
|
|||
|
Thanks again
Thanks yet again, Im not sure if I like Python. C and Java seem to be alot better IMO??? Is Python better for AI or something (thats the class that requires me to use it)
|
|
#7
|
|||
|
|||
|
Quote:
The equivalent Java or C++ code would behave in exactly the same way. Try it in Java - (I will just give the pseudocode, since it is years since I have used Java, and I do not have time to spend looking up Java syntax & library APIs): Code:
create a Vector object A (or other java collection) add another Vector object B to it pass it to a function modify either A or B in the function check the Vector outside the function - you will see that it has changed. IMHO Python is a better language for most programming tasks. The code to do the above will be several times longer than the Python code, and much less readable/maintainable. Dave |
|
#8
|
||||
|
||||
|
Quote:
Are you, perhaps, writing "Java-in-Python" or "C-in-Python"? (A phenomenon where people take Java code and distort it into Python syntax until it runs. Then they say "Python is ugly!". Solution: Make it Pythonic.) Quote:
Your choices are either: - Pass by value. Every time you call a function, a full copy is made of all the parameters. Slow and very memory-hungry. - Pass by reference (explicitly). Every time you call a function, you have to say how to pass each parameter. Most of the time you want to pass by reference. Verbose and ugly ("ByRef x as String" in VB). - Pass by reference (implicitly). Every time you call a function, parameters are passed by reference. Simple, memory-friendly, mostly what you want, but can occasionally lead to problems like you are having when you need to explicitly copy the objects. Python uses references unless told not to because that's what most people need most of the time. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Problem with referencing |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|