Question presented to candidate:
"If I write obj.method without calling it, what do I actually get back? What is a bound method, what did 'unbound method' mean, and how does self get passed in?"
What a strong answer should cover:
- A bound method is an object that pairs a function with an instance:
obj.methodgives amethodobject with__self__(the instance) and__func__(the function); calling it calls__func__(__self__, *args). - In Python 3,
Class.methodis just the plain function; the separate "unbound method" type of Python 2 (which type-checked its first argument) was removed in 3.0. - Binding is done by the descriptor protocol: functions define
__get__, so attribute lookup through an instance callsfunction.__get__(obj, type(obj)). - Every access creates a new bound method object (
obj.m is obj.misFalse, but==isTrue); CPython avoids that allocation for direct calls withLOAD_METHOD(3.11) /LOAD_ATTRmethod form (3.12+). - Bound methods are first-class callables (callbacks,
map), and they hold a strong reference to the instance;weakref.WeakMethodavoids keeping it alive. - Functions stored on an instance are not bound;
types.MethodTypebinds one by hand.
Code / implementation expected: Optional β show type(C.f) vs type(c.f), __self__/__func__, and C.f(c) being the same as c.f().