Questions
10 of 19
1How would you make a custom class usable in a for loop?
2How do __enter__ and __exit__ work together to implement the context manager protocol?
3How does Python implement inheritance, and what is the Method Resolution Order (MRO)?
4What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?
5What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?
6What is the difference between method overriding and method overloading, and why doesn't Python natively support overloading?
7What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?
8What is the difference between a class and an instance in Python?
9How does Python implement encapsulation given that it has no true 'private' access modifiers?
10What is the purpose of self in instance methods, and why must it be explicitly declared in Python?
11What is the difference between __str__ and __repr__, and what convention should __repr__ follow?
12What is a metaclass, and what is type's relationship to every class in Python?
13What is the purpose of __eq__ and __hash__, and why must they be implemented consistently?
14What is a descriptor, and how do @property and ORMs like Django's models rely on the descriptor protocol?
15What is the difference between an instance method, a class method, and a static method?
16What is composition, and why is 'favor composition over inheritance' often recommended?
17What are dunder (magic) methods, and how do they enable operator overloading?
18When would you use a metaclass instead of a class decorator or __init_subclass__?
19How do abstract base classes (abc.ABC) enforce interface contracts in Python?
10 / 19

What is the purpose of self in instance methods, and why must it be explicitly declared in Python?

Difficulty: 2/10
Self, Descriptors, Method Binding

self is the explicit first parameter; binding happens through the descriptor protocol, not a keyword

self is just a conventional name for the first parameter of an instance method. It is not a keyword. When you write obj.method(x), Python first looks up method on the class, finds a function object, and because functions implement get, it returns a bound method that already has obj as the first argument. So obj.method(x) is effectively Class.method(obj, x). Python requires the first parameter to be explicit for readability and because functions are plain objects; there is no hidden this. This also means you can call the underlying function directly with an explicit instance, which is what super() and unbound-method patterns exploit.

  1. 1

    self is not special to the parser; it is special only by convention. Naming it cls in a classmethod is also convention.

  2. 2

    Binding happens via functions being descriptors: function.get(instance, owner) returns a method object.

  3. 3

    Trade-off: explicitness makes it clear what is bound, but it adds visual noise and is the most common source of TypeError: missing 1 required positional argument.

  4. 4

    Common mistake: thinking you must name it self. You can name it anything, but doing so breaks readability and tooling assumptions.

  5. 5

    Common mistake: calling Class.method(x) on an instance method and forgetting that you must pass the instance explicitly.

  6. 6

    Version note: unbound method objects from Python 2 were removed in Python 3; functions are simply descriptors now.

javascript

Scenario Questions

0-2 years experience

  1. 1You call Class.method(arg) and get a missing positional argument error. What happened?
  2. 2Is self a reserved keyword in Python?

2-5 years experience

  1. 1You store a method in a variable and call it later. Does binding still happen?
  2. 2You need to call a parent method explicitly without using super(). How do you pass the instance?

5-8 years experience

  1. 1You attach a plain function as an instance attribute and expect binding. Why does it fail and how do you emulate binding?
  2. 2How does the descriptor protocol decide between a function, a property, and a classmethod when accessed on an instance?

8+ years experience

  1. 1Design a method decorator that preserves binding semantics and remains inspectable under mypy and IDEs.
  2. 2Explain how CPython's method object caches __self__ and __func__, and how that affects performance and pickling.

Follow-up Questions

  • Why can you call Counter.incr(c) directly and what does that reveal about binding?
  • What does super() return and how does it use the instance?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.