reference

How do I clone a list so that it doesn't change unexpectedly after assignment?

How do I clone a list so that it doesn't change unexpectedly after assignment? Question: While using new_list = my_list, any modifications to new_list changes my_list every time. Why is this, and how can I clone or copy the list to prevent it? Asked By: aF. || Source Answers: Use thing[:] >>> a = [1,2] …

Total answers: 24

referencing class methods in class lists in Python

referencing class methods in class lists in Python Question: I am writing a class in Python 2.6.2 that contains a lookup table. Most cases are simple enough that the table contains data. Some of the cases are more complex and I want to be able call a function. However, I’m running into some trouble referencing …

Total answers: 7

How do I pass a variable by reference?

How do I pass a variable by reference? Question: Are parameters passed by reference or by value? How do I pass by reference so that the code below outputs ‘Changed’ instead of ‘Original’? class PassByReference: def __init__(self): self.variable = ‘Original’ self.change(self.variable) print(self.variable) def change(self, var): var = ‘Changed’ See also: Why can a function modify …

Total answers: 40

Is there a difference between "==" and "is"?

Is there a difference between "==" and "is"? Question: My Google-fu has failed me. In Python, are the following two tests for equality equivalent? n = 5 # Test one. if n == 5: print ‘Yay!’ # Test two. if n is 5: print ‘Yay!’ Does this hold true for objects where you would be …

Total answers: 14