Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 30th, 2004, 05:10 PM
Scorpionb Scorpionb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 Scorpionb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old September 30th, 2004, 05:29 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Click here for more information.
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,717 Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 12 h 29 m 35 sec
Reputation Power: 1179
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

Reply With Quote
  #3  
Old September 30th, 2004, 05:44 PM
Scorpionb Scorpionb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 Scorpionb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old September 30th, 2004, 07:59 PM
Scorpionb Scorpionb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 Scorpionb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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)

Reply With Quote
  #5  
Old September 30th, 2004, 08:06 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Click here for more information.
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,717 Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 12 h 29 m 35 sec
Reputation Power: 1179
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!

Reply With Quote
  #6  
Old September 30th, 2004, 08:09 PM
Scorpionb Scorpionb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 Scorpionb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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)

Reply With Quote
  #7  
Old October 1st, 2004, 03:36 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
Quote:
Originally Posted by Scorpionb
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)


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

Reply With Quote
  #8  
Old October 1st, 2004, 11:36 AM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 10
Quote:
Originally Posted by Scorpionb
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)


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:
I disagree with that aspect of having to do something like that

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Problem with referencing


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT