Python: Create a class that supports unpacked assignment

Make a note of the result of trial and error, wondering how the unpacked substitution was realized.

Implement __getitem__ ()

>>> class MyTuple:
...     def __getitem__(self, key):
...         print(key)
...         if key < 3:
...             return str(key)
...         else:
...             raise IndexError
...

It seems that the __getitem__ () method is used for unpacked assignment [^ 1]. The index number is passed as the key.

[^ 1]: I searched for the document, but it didn't come out at once, so I experimented and looked it up.

For ordinary assignment

__getitem__ () is not called.

>>> x = MyTuple()
>>> x
<__main__.MyTuple object at 0x10a6bd850>

For unpacked assignments with few elements

__Getitem__ () is called for the number of elements on the left side + 1 time. If ʻIndexError does not occur in any of the calls, it is assumed that there are many elements on the right side, and ValueError` occurs.

>>> x, y = MyTuple()
0
1
2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

For unpacked assignments with matching number of elements

__Getitem__ () is called for the number of elements on the left side + 1 time. If the last call raises ʻIndexError`, it assumes that the left and right elements match and the assignment succeeds.

>>> x, y, z = MyTuple()
0
1
2
3
>>> x, y, z
('0', '1', '2')

For unpacked assignments with many elements

__Getitem__ () is called for the number of elements on the left side + 1 time. If ʻIndexError` occurs in the middle of the call, it is considered that the element on the right side is missing and the assignment fails.

>>> v, x, y, z = MyTuple()
0
1
2
3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 4, got 3)

For unpacked assignments with *

If there is an element with * on the left side, __getitem__ () will be called until ʻIndexError` occurs [^ 2]. Substitute the result into the variable on the left side nicely.

[^ 2]: Good feeling is PEP 3132 --Extended Iterable Unpacking.

>>> a, *b = MyTuple()
0
1
2
3
>>> a, b
('0', ['1', '2'])

A nice example

>>> *a, b = MyTuple()
0
1
2
3
>>> a, b
(['0', '1'], '2')

It makes you feel like you don't have to have one element.

>>> *w, x, y, z = MyTuple()
0
1
2
3
>>> w, x, y, z
([], '0', '1', '2')
>>> w, *x, y, z = MyTuple()
0
1
2
3
>>> w, x, y, z
('0', [], '1', '2')
>>> w, x, *y, z = MyTuple()
0
1
2
3
>>> w, x, y, z
('0', '1', [], '2')
>>> w, x, y, *z = MyTuple()
0
1
2
3
>>> w, x, y, z
('0', '1', '2', [])

But if you don't have two, you can't.

>>> *v, w, x, y, z = MyTuple()
0
1
2
3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected at least 4, got 3)

Implement __iter__ ()

You can also unpack assign by implementing __iter__ () instead of __getitem__ ().

>>> class MyTuple2():
...     def __iter__(self):
...         return iter([1, 2, 3])
...

The implementation is pretty broken, but it returns an iterator for an array with three elements. It works properly.

>>> x, y, z = MyTuple2()
>>> x, y, z
(1, 2, 3)

Summary

Either __getitem__ () or __iter__ () can be used, which means that iterable is unpacked. It can be rephrased as substitutable.

Examples of repeatable objects include all sequence types (list, str, tuple, etc.), some non-sequence types, such as dict and file objects, or the __iter__ () method or which implements Sequence semantics. Contains an instance of any class that has the __getitem__ () method.

Recommended Posts

Python: Create a class that supports unpacked assignment
[Python] Create a LineBot that runs regularly
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
Create a Python environment
I tried to create a class that can easily serialize Json in Python
Create a fake class that also cheats is instance
Create a chatbot that supports free input with Word2Vec
MALSS, a tool that supports machine learning in Python
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
Create a directory with python
Note that it supports Python 3
[Python] Create a linebot that draws any date on a photo
Let's create a script that registers with Ideone.com in Python.
Creating a Python script that supports the e-Stat API (ver.2)
Create code that outputs "A and pretending B" in python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
How to write a metaclass that supports both python2 and python3
How to write a Python class
Create a python GUI using tkinter
Create a DI Container in Python
Create a Python environment on Mac (2017/4)
Create a virtual environment with Python!
Create a binary file in Python
Create a python environment on centos
Create a Python general-purpose decorator framework
Create a Kubernetes Operator in Python
5 Ways to Create a Python Chatbot
Create a random string in Python
[Python] Inherit a class with class variables
Create an instance of a predefined class from a string in Python
[Python / Django] Create a web API that responds in JSON format
[Ev3dev] Create a program that captures the LCD (screen) using python
[LINE Messaging API] Create a BOT that connects with someone with Python
Let's create a Python directory structure that you won't regret later
A class that summarizes frequently used methods in twitter api (python)
[python] I made a class that can write a file tree quickly
Generate a first class collection in Python
[Python] A program that creates stairs with #
Create a new Python numerical calculation project
A class that hits the DMM API
[Python] How to make a class iterable
Create a dummy image with Python + PIL.
Create a python environment on your Mac
Let's create a virtual environment for Python
Create a JSON object mapper in Python
[Python] [LINE Bot] Create a parrot return LINE Bot
Create a word frequency counter with Python 3.4
Technology that supports Python Descriptor edition #pyconjp
Create a deb file from a python package
[GPS] Create a kml file in Python
Generate a class from a string in Python
A typed world that begins with Python
Let's create a customer database that automatically issues a QR code in Python
Supports Python 2.4
Create a bot that boosts Twitter trends
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
From a book that programmers can learn (Python): Class declaration (public / private, etc.)