liveBook · Manning

Initializer

As explained in Python How-To

A constructor is a callable that creates an instance of a class or other typed object.

How do you create a Python object and give it its starting attributes? Call its class, passing the values it needs. The call constructs an instance; the class’s `__init__` method initializes that instance. Built-in classes such as `dict`, `set`, `int`, and `float` can also be called to create instances. section 5.1 section 10.2.1 section 2.2.2

Initialize a task

class Task: def __init__(self, title): self.title = title task = Task("Laundry") print(task.title) # Laundry `self` refers to the new instance and is supplied implicitly; the caller supplies `title`. Initialize an instance’s attributes in `__init__`, even when an initial value must be `None`. section 10.2.1 section 8.1.1 section 8.6

There is a useful distinction behind that simple call: `__new__` creates and returns the instance first; Python then passes that same instance to `__init__`, which sets its attributes in place. You normally do not need to implement `__new__` or call either method manually. `__init__` is the initializer, not the step that creates the object. section 10.2.1 section 8.1.1

Choose the shortest readable construction pattern for the job:

  • Use a built-in constructor directly when an iterable needs no item-by-item transformation: `set([1, 1, 2, 3])` creates a set. section 5.2.6
  • Pair keys and values before constructing a dictionary: `dict(zip(id_numbers, titles))`. This avoids a manual indexing loop. section 5.1 section 11.2.2
  • Use `float("3.25")` or `int("123")` to create numeric objects from valid strings. Invalid strings raise `ValueError`; handle it when conversion may fail. section 2.2.2

When reading CSV rows with a header, `dict(zip(fields, row))` constructs each record from paired field names and values. For that specific task, `csv.DictReader` is a cleaner option. Prefer direct constructors when their inputs already have the needed shape; use comprehensions when items need manipulation. section 11.2.2 section 5.2.6

How Python How-To covers it

How deep it goes
Explains class calls through the sequential roles of `__new__` and `__init__`, while noting that custom `__new__` implementations are usually unnecessary.
Examples
Uses task initialization, set construction, numeric-string conversion, and CSV rows converted to dictionaries.
What it assumes
Introduces instance, instantiation, and constructor terminology while using familiar classes, iterables, and function calls.
What you can do after
Build it from scratch by defining `__init__`, and apply built-in constructors directly when their inputs already fit.
How it connects
Connects instantiation to iterables, comprehensions, casting, instance attributes, and exception handling.

Also explained in

Compare how 9 books explain initializer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.