liveBook · Manning

__getitem__

__getitem__ is a Python special method that defines subscription access to an object's elements using square-bracket notation.

How 2 Manning books explain it

The Quick Python Book, Fourth Edition

__getitem__ is a Python special method that defines how an object responds when an item or slice is accessed with subscription syntax.

How it is used
Enables user-defined objects to respond to item access and participate in sequence-style iteration.
How deep it goes
Explains subscription dispatch and iteration termination, then extends the approach toward list emulation without demonstrating slice handling.
Examples
Uses a sequential file reader and a type-restricted list to demonstrate custom access.
What you can do after
Build it from scratch in a user-defined class for sequential iteration or delegated item access.

Read how The Quick Python Book, Fourth Edition explains __getitem__ →

Deep Learning with PyTorch, Second Edition

A Python special method that retrieves an item by index or key, used here by Dataset to return a sample and label.

How it is used
Provides indexed access to individual samples for PyTorch Dataset objects used in image classification and custom training or validation datasets.
How deep it goes
Defines the index contract and demonstrates sample construction, tensor conversion, dimensions, and multi-value returns without descending into method internals.
Examples
Uses CIFAR-10 image access and a LunaDataset tuple containing tensors, a series identifier, and coordinates.
What you can do after
Apply it via PyTorch Dataset subclassing to retrieve and format indexed samples for training or validation.

Read how Deep Learning with PyTorch, Second Edition explains __getitem__ →

Also covered in

Related concepts

Questions about __getitem__

What is __getitem__?

__getitem__ is a Python special method that defines subscription access to an object's elements using square-bracket notation.

Which Manning books explain __getitem__?

The Quick Python Book, Fourth Edition and Deep Learning with PyTorch, Second Edition.

What is __getitem__ a kind of?

__getitem__ is a kind of function. A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.