On Friday, November 18, 2016 at 9:22:27 AM UTC-8, tomdean1939 wrote: > > I assign the contents of one variable to another variable and then append > to that variable. The original variable is changed. > > for u in oddone: > thishand = fourofkind > print " append ",u," to ",thishand > thishand.append(u) > Hands.append(thishand) > > after this, fourofkind is changed. >
That's how python works: variables are *bound to* an object and the assignment operation is really "bind LHS to RHS". So after thishand = fourofkind both variables are bound to the same list. the command thishand.append(u) really means "append u to the object that "thishand" is bound to. > What am I doing wrong? > If you want thishand and fourofkind to be bound to different objects, use thishand = copy(fourofkind) or (this seems to be slightly more efficient) thishand=fourofkind[:] If you have lists you are sure you don't want to change, consider them making tuples instead. -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.
