Making List-Like Objects in Python - The Right Way

 Making List-Like Objects in Python - The Right Way

In this post, we will be talking about how Python likes to deal with "list-like objects". We will be diving into some quirks of Python that might seem a bit weird and, in the end.

In this post, we will be talking about how Python likes to deal with "list-like objects". We will be diving into some quirks of Python that might seem a bit weird and, in the end, we will hopefully teach you how to build something that could actually be useful while avoiding common mistakes.

Part 1: Fake lists

Let's start with this snippet.

class FakeList:
    def __getitem__(self, index):
        if index == 0:
            return "zero"
        elif index == 1:
            return "one"
        elif index == 2:
            return "two"
        elif index == 3:
            return "three"
        elif index == 4:
            return "four"
        elif index == 5:
            return "five"
        elif index == 6:
            return "six"
        else:
            raise IndexError(index)

f = FakeList()

A lot of people will be familiar with this:

f[3]
## <<< 'three'

__getitem__is the method you override if you want your instances to respond to the square bracket notation. Essentiallyf[3]is equivalent tof.__getitem__(3).

python list-like-objects code coding lists hackernoon-top-story python-programming-lists good-company

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Find Ulimit For user on Linux

Explains how to find ulimit values of currently running process or given user account under Linux using the 'ulimit -a' builtin command.

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS - We are going to build a full stack Todo App using the MEAN (MongoDB, ExpressJS, AngularJS and NodeJS). This is the last part of three-post series tutorial.

systemctl List All Failed Units/Services on Linux

Explains how to use the systemctl command to list all failed units or services on Debian, Ubuntu, CentOS, Arch, Fedora, and other Linux distros.

Docker Applikationen mit Visual Studio Code debuggen

Mit dem integrierten Debugger von Visual Studio Code lassen sich ASP.NET Core bzw. .NET Core Applikationen einfach und problemlos debuggen. Der Debugger unterst├╝tzt auch Remote Debugging, somit lassen sich zum Beispiel .NET Core Programme, die in einem Docker-Container laufen, debuggen.

How to configure AWS SES with Postfix MTA

Amazon Simple Email Service (SES) is a hosted email service for you to send and receive email using your email addresses and domains. Typically SES used for sending bulk email or routing emails without hosting MTA. We can use Perl/Python/PHP APIs to send an email via SES. Another option is to configure Linux or Unix box running Postfix to route all outgoing emails via SES. Before getting started with Amazon SES and Postfix, you need to sign up for AWS, including SES. You need to verify your email address and other settings. Make sure you create a user for SES access and download credentials too.