skip to content

Classes vs Instances

A class is a template and a type; its instances are the runtime things that carry per-instance state. Interviewers ask it to check you separate compile-time descriptions from runtime values.

on this pageshow

questions

3

A runtime type test -- Python's isinstance, a Go type assertion on an interface, JavaScript's instanceof -- appears to ask the same question of an object. What is each one actually checking, and where do they give different answers for objects of the same shape?

level: seniorimportance: should knowfreq 42%

basics

~20 s

They ask three different questions. Python's isinstance asks declared-or-registered membership, and a metaclass hook can make it say yes without inheritance. Go asks whether the concrete type's method set covers the interface, with no declaration anywhere. JavaScript's instanceof asks whether one specific constructor's prototype object is on the chain, so it fails across realms.

open as a page

Some runtimes let a running program manufacture a brand-new class: Python's built-in `type(name, bases, namespace)`, Ruby's `Class.new`, and the Objective-C runtime's `objc_allocateClassPair` + `objc_registerClassPair` pair. Several also fire a hook the instant a subtype appears — Python's `__init_subclass__`, Ruby's `inherited`. Other platforms fix the set of types before the program starts: Rust has no runtime class construction, Go's `reflect.StructOf` can fabricate a struct shape but cannot attach methods to it, and a GraalVM native-image binary only contains types the build could see. What does runtime type construction buy a framework author, what does closing the type set buy in return, and how do you decide which cost your system pays?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Runtimes that can build a class while running — Python's type(), Ruby's Class.new, Objective-C class pairs — let frameworks derive types from data and let subtypes self-register. Closing the type set instead buys ahead-of-time layout, dead-code stripping, and types you can actually see.

open as a page