Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 18 Online
OpenStudy (anonymous):

I'm on problem 5 of problem set 3a. Whenever the user plays a hand it runs the hand through the update hand function from problem 2. How do I get the original hand back for when the user chooses to replay the hand? It seems like it should be easy but I'm having trouble with this part.

OpenStudy (turingtest):

you should make a clone of the dictionary with your hand and alter the original

OpenStudy (turingtest):

You have to clone dictionaries to avoid aliasing problems, I am just guessing that may be your issue. Seeing your code would probably help me be more specific.

OpenStudy (anonymous):

I used the copy module and copy.deepcopy() and it works. Is this what you meant by making a clone or is that something else I should also know about? Thanks for the input!

OpenStudy (turingtest):

I think a shallow `foo.copy()` would have been sufficient, but yes and you're welcome :)

OpenStudy (turingtest):

what you did is called "aliasing," if you did something like ``` foo = {'a':1,'b':2} bar = foo bar['a'] = 3 ``` then you have also changed `foo` as well as `bar`, because `foo` and `bar` both point to the same object. I don't think I can explain the reasoning behind this in python very well. My understanding of these kinds of issues is based on pointers, but I have yet to have to deal with pointers in any language besides C++. Someone more experienced than me should explain why mutable types alias in python.

OpenStudy (turingtest):

`bar = foo.copy()` creates an entirely new object, so there is no aliasing issue here

OpenStudy (anonymous):

Oh I see. Thanks

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!