Skip to the page

Core data model · core

is versus ==, and how arguments work

is checks that two names point to the same object, == checks equality through eq. Function parameters can be positional-only, normal, *args, keyword-only or **kwargs, and each form has a reason.

The notes

What you must know

  • is checks identity; == calls eq, which a class can override. Use is for None, True, False and sentinel objects.
  • CPython caches small ints (-5 to 256) and interns some strings, so is sometimes works on equal values. That is an implementation detail and Python gives a SyntaxWarning for is with a literal.
  • x == None can be fooled by a custom eq, and NumPy arrays return an array from ==. Write x is None.
  • if not x is true for 0, "", [] and None alike, so it is wrong when 0 is a valid value.
  • NaN is not equal to itself, but x in [x] is True because containers check identity before equality.
  • *args collects extra positional arguments into a tuple and **kwargs collects extra keyword arguments into a dict. At a call site, * and ** unpack.
  • In def f(a, b, /, c, *, d): a and b are positional-only, c can be either, d is keyword-only. Positional-only lets a library rename a parameter safely. Keyword-only makes calls readable (send(msg, *, retry=True)).
  • **kwargs swallows typos silently. Prefer explicit parameters, or TypedDict with Unpack for typed keyword arguments.

The red pen

Common traps: mistake → fix

  • Mistake: is is just a faster ==

    Fix: is tests identity, so equal strings or numbers may fail it.

  • Mistake: args and kwargs are keywords

    Fix: they are only conventional names; the stars matter.

  • Mistake: Passing everything through **kwargs is flexible

    Fix: after several layers nobody knows which arguments are valid.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between is and ==?
  2. Why can a is b be True for 256 but False for 1000?
  3. Why do we write if x is None instead of if x == None?
  4. What do the slash and the star mean in def f(a, b, /, c, *, d)?
  5. What is wrong with passing **kwargs through five layers?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview