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.
you should make a clone of the dictionary with your hand and alter the original
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.
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!
I think a shallow `foo.copy()` would have been sufficient, but yes and you're welcome :)
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.
`bar = foo.copy()` creates an entirely new object, so there is no aliasing issue here
Oh I see. Thanks
Join our real-time social learning platform and learn together with your friends!