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








