#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep 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
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep 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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep 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)
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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!
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep 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)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    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.)

    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.

IMN logo majestic logo threadwatch logo seochat tools logo