Skip to content
C

Python Interview Questions

Typing & Modern Python Syntax Interview Questions

namedtuple, Protocol, Optional/Union, match-case, the walrus operator, and positional-/keyword-only parameters.

Question 1: What is a namedtuple?

Ans

collections.namedtuple creates tuple subclasses whose fields can be accessed by name as well as position.

Example

python
from collections import namedtuple Point = namedtuple("Point", "x y") p = Point(10, 20) print(p.x, p.y)

Important Point

For new code, dataclasses or typing.NamedTuple may be preferable depending on requirements.

Question 2: What is typing.Protocol?

Ans

A Protocol defines a structural interface for static type checkers: a type can satisfy the protocol by providing the required members without explicitly inheriting from it.

Example

python
from typing import Protocol class HasName(Protocol): name: str

Important Point

Protocol mainly improves static typing; Python's runtime behavior still follows normal duck typing unless runtime_checkable is used.

Question 3: What is Optional in typing?

Ans

Optional[T] traditionally means a value may be T or None; modern Python style often writes the same idea as T | None.

Example

python
def find_name() -> str | None: return None

Important Point

Type annotations do not automatically enforce that a function actually returns the annotated type.

Question 4: What is Union type?

Ans

A union type represents a value that may have one of several types. Modern Python can express this with A | B.

Example

python
def format_id(value: int | str) -> str: return str(value)

Important Point

A union describes possible types; runtime code still needs correct handling for operations not shared by all members.

Question 5: What is match-case?

Ans

Structural pattern matching, introduced in Python 3.10, lets code match values and structures using match and case.

Example

python
def status(code): match code: case 200: return "OK" case 404: return "Not Found" case _: return "Other"

Important Point

Pattern matching is more powerful than a simple switch because it can match structure and bind values.

Question 6: What is walrus operator?

Ans

The assignment expression operator := assigns a value as part of an expression.

Example

python
if (n := len("Python")) > 5: print(n)

Important Point

Use it when it improves clarity; overusing assignment expressions can make code harder to read.

Question 7: What is positional-only parameter?

Ans

A parameter before / in a function signature can only be supplied positionally.

Example

python
def power(base, exp, /): return base ** exp print(power(2, 3))

Important Point

Callers cannot use `base=...` for a positional-only parameter.

Question 8: What is keyword-only parameter?

Ans

A parameter after * in a function signature must be passed by keyword.

Example

python
def connect(host, *, timeout=10): print(host, timeout) connect("localhost", timeout=5)

Important Point

Keyword-only parameters can make APIs clearer and prevent ambiguous positional calls.

Question 9: What is function annotation?

Ans

A function annotation attaches metadata such as expected argument and return types to a function definition.

Example

python
def greet(name: str) -> str: return f"Hello {name}" print(greet.__annotations__)

Important Point

Annotations are available at runtime as metadata but are not normally runtime type enforcement.

Question 10: What is inspect module?

Ans

The inspect module provides tools for examining live Python objects, signatures, source information, and call frames.

Example

python
import inspect def add(a, b): return a+b print(inspect.signature(add))

Important Point

Introspection can be affected by decorators, generated functions, or unavailable source code.

Question 11: What is property setter?

Ans

A property setter defines what happens when code assigns to a property, allowing validation or transformation while keeping attribute syntax.

Example

python
class User: def __init__(self, age): self.age = age @property def age(self): return self._age @age.setter def age(self, value): if value < 0: raise ValueError("age") self._age = value

Important Point

Keep property setters predictable; surprising side effects make attribute assignment harder to understand.

Question 12: What is Protocol vs ABC?

Ans

ABC provides nominal inheritance-based contracts and can enforce abstract methods at instantiation. Protocol supports structural typing for static analysis, so compatible classes need not inherit from it.

Example

python
from typing import Protocol class HasSave(Protocol): def save(self) -> None: ...

Important Point

Choose based on whether explicit runtime/nominal contracts or structural typing is the better fit.

Continue Your Preparation