Questions
8 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?
08 / 19

What is the difference between a class and an instance in Python?

Difficulty: 2/10
Classes, Instances, Attribute Lookup

A class is a callable object that produces instances; each instance has its own dict and shares the class's attributes

A class is itself an object: it is created by the class statement (or by calling type()) and is an instance of a metaclass. It defines shared attributes and behavior in its own dict. An instance is a separate object produced by calling the class. Attribute lookup on an instance first checks the instance dict, then the class and its MRO. That is why class attributes are shared by default and assigning to self.attr creates a per-instance attribute that shadows the class one, whereas assigning to ClassName.attr changes it for everyone that has not shadowed it.

  1. 1

    Class: blueprint plus runtime object; stores methods, class variables, and annotations.

  2. 2

    Instance: independent namespace; stores per-object state in obj.dict.

  3. 3

    Mutable class attributes are the classic trap: a list defined on the class is shared by every instance unless each instance assigns its own.

  4. 4

    Trade-off: shared class state saves memory and enables registry patterns; per-instance state is safer for anything mutable.

  5. 5

    Common mistake: assuming methods are copied into each instance. They live on the class and are bound on access through the descriptor protocol.

  6. 6

    Version note: since Python 3, all classes are new-style; the old-style class distinction from Python 2 is gone.

javascript

Scenario Questions

0-2 years experience

  1. 1You define counters as a class attribute list and every instance sees the same values. Why, and how do you fix it?
  2. 2Where does Python look first when you access obj.attr?

2-5 years experience

  1. 1You want a shared default configuration but per-instance overrides. How do you structure the class?
  2. 2A subclass reassigns a parent class attribute. Which instances see the new value?

5-8 years experience

  1. 1You need a registry of every instance created. Where do you store it and how do you avoid memory leaks?
  2. 2You are designing a plugin system where shared class-level state must remain consistent under concurrent registration. How do you handle this?

8+ years experience

  1. 1Design an object model that shares immutable defaults at the class level while allowing cheap per-instance overrides at scale.
  2. 2How do slots, __dict__, and class-level attributes interact for memory and lookup performance in CPython?

Follow-up Questions

  • Why is defining a mutable list as a class attribute risky?
  • How does method binding work if methods are stored on the class rather than the instance?
Share

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