0

In Python 3, I tried the following code and the result is shown in comments:

f = max
max = min
print(max(3,4))       # ① prints 3
print(f(3,4))         # ② prints 4
print(max == min)     # ③ prints True
print(max == f)       # ④ prints False
print(min == f)       # ④ prints False

I have the following questions:

  1. I understand the result of ①. It prints 3 because the name max is now bound to what the build-in min function does. Is that right?
  2. For result of ②, f still acts as a max even though the max is now actually a min. Is this because that the first line of binding f = max actually does not bind f to the name max but rather to the content of max? (Somewhat like pass by value as opposed to pass by reference)
  3. For result of ④ and ⑤, why both of them are False? I thought at least one of them will be True.

4 Answers 4

3
  1. Yes.

  2. Correct, f is bound to the thing that max was bound to at the time you made the assignment.

  3. f references the builtin max function, and max and min both reference the builtin min function. Hence neither max nor min are the same as f.

>>> f = max
>>> max == f
True
>>> max = min
>>> max == f
False

Note that you can explore these same concepts much more easily with regular old ints:

>>> a = 2
>>> b = a
>>> a = 3
>>> a, b
(3, 2)
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I still don't get question number 3. What exactly does == do? Does it compare reference or content?
obj1 == obj2 is the same as obj1.__eq__(obj2). I believe function objects are compared by their id rather than the "content" of the functions. But I think you're getting lost in the weeds here -- look at the second example with a and b which are regular old numbers. Setting a = 3 does not change the fact that b still points to 2. That's it, that's the whole thing. 2 == 3 is False -- you don't need to get into the details of the implementation of == to understand why that's the case. :)
Yes. I get it now. Thank you!
2

With regard to (1) and (2), your understanding is correct.

With regard to (3), both max and min now reference the built-in function min, whereas f references the built-in function max. Therefore, both comparisons return False.

3 Comments

Does that mean the = works differently on functions and objects (I thought functions in Python are objects too)? For object, o2 = o1 simply gives another name to the object o1 is pointing to. Both o2 and o1 points to the same object. For function, f = max creates a new function object with the same content as max and assign it to f. So, rebinding max to min later does not affect f. Is that right?
@LongYu you're describing the same behavior. f = max means that the name f points to the object (function) max points to. Then you have max = min that makes max point to a different object (function), so now f == max is False.
Get it now. Thank you!
1

f = max The name f is now bound to the maximum function

max = min The name max is now bound to the minimum function

Changing the value of max won't change the value of f so it is still bound to the maximum function therefore max == min != f

Comments

1

Variables reference objects and keep no memory of where those objects may have come from. In your case, the variables reference function objects. The function objects themselves are anonymous. When you execute

f = max

max is referencing a function object. After assignment, f also references that same function object whose reference count is incremented by 1. You can now do anything you like to the variable max. The function object will have its reference count decremented by 1, but since its also referenced by f, it still hangs around.

Python, being the friendly language that it is, stamps that function object with its first reference when compiled. You can use that in a print statement to see the origins of the function object.

print('max', max)
f = max
print('f', f)
max = min
print('max', max)
print(max, max(3,4))       # ① prints 3
print(f, f(3,4))         # ② prints 4
print(f, max == min)     # ③ prints True
print(max, f, max == f)       # ④ prints False
print(min, f, min == f)       # ④ prints False

Results in

max <built-in function max>
f <built-in function max>
max <built-in function min>
<built-in function min> 3
<built-in function max> 4
<built-in function max> True
<built-in function min> <built-in function max> False
<built-in function min> <built-in function max> False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.