reference

In Python, when are two objects the same?

In Python, when are two objects the same? Question: It seems that 2 is 2 and 3 is 3 will always be true in python, and in general, any reference to an integer is the same as any other reference to the same integer. The same happens to None (i.e., None is None). I know …

Total answers: 2

Why does Python handle '1 is 1**2' differently from '1000 is 10**3'?

Why does Python handle '1 is 1**2' differently from '1000 is 10**3'? Question: Inspired by this question about caching small integers and strings I discovered the following behavior which I don’t understand. >>> 1000 is 10**3 False I thought I understood this behavior: 1000 is to big to be cached. 1000 and 10**3 point to …

Total answers: 1

How to create a reference to a variable in python?

How to create a reference to a variable in python? Question: In the code: y = 7 x = y x = 8 Now, y will be 7 and x will be 8. But actually I wan to change y. Can I assign the reference of y and do that? For example, in C++ the …

Total answers: 4

Python: short coordinate function unexpectedly changes incoming argument value

Python: short coordinate function unexpectedly changes incoming argument value Question: The goal here is to take some list of coordinates, like [[1,2],[3,4],[7,1]], then figure out how big the canvas should be if you want to print all these coords. Take the maximal bottom left coordinate and minimal upper right coordinate that will snugly fit a …

Total answers: 2

Python : When is a variable passed by reference and when by value?

Python : When is a variable passed by reference and when by value? Question: My code : locs = [ [1], [2] ] for loc in locs: loc = [] print locs # prints => [ [1], [2] ] Why is loc not reference of elements of locs ? Python : Everything is passed as …

Total answers: 6

python list by value not by reference

python list by value not by reference Question: Let’s take an example a=[‘help’, ‘copyright’, ‘credits’, ‘license’] b=a b.append(‘XYZ’) b [‘help’, ‘copyright’, ‘credits’, ‘license’, ‘XYZ’] a [‘help’, ‘copyright’, ‘credits’, ‘license’, ‘XYZ’] I wanted to append value in list ‘b’ but the value of list ‘a’ have also changed. I think I have little idea why its …

Total answers: 11

Python List & for-each access (Find/Replace in built-in list)

Python List & for-each access (Find/Replace in built-in list) Question: I originally thought Python was a pure pass-by-reference language. Coming from C/C++ I can’t help but think about memory management, and it’s hard to put it out of my head. So I’m trying to think of it from a Java perspective and think of everything …

Total answers: 3

Testing for reference equality in Python

Testing for reference equality in Python Question: Say I have a class in Python that has an eq method defined for comparing attributes for equality: class Foo(object): # init code… def __eq__(self, other): # usual eq code here…. How can I then compare two instances of Foo for reference equality (that is test if they …

Total answers: 3

Is there a generic way for a function to reference itself?

Is there a generic way for a function to reference itself? Question: I can access a python function’s attribute inside of function itself by below code: def aa(): print aa.__name__ print aa.__hash__ # other simliar However, if above aa() function is a template for write other code, say bb(), I have to write: def bb(): …

Total answers: 4