Telegram Group & Telegram Channel
Отсутствующие ключи в словаре

При обращении по отсутствующему ключу в словаре мы получим исключение KeyError. Но мы можем переопределить это поведение.

Для этого создадим свой собственный класс словаря, унаследовавшись от dict. После чего нам просто необходимо переопределить метод __missing__, который вызывается в базовой реализации методом dict.__getitem__ при при адресации к отсутствующему ключу.

Мы можем, например, возвращать какое-то дефолтное значение, либо определить любую другую необходимую логику поведения.

#dict



tg-me.com/python_academy/675
Create:
Last Update:

Отсутствующие ключи в словаре

При обращении по отсутствующему ключу в словаре мы получим исключение KeyError. Но мы можем переопределить это поведение.

Для этого создадим свой собственный класс словаря, унаследовавшись от dict. После чего нам просто необходимо переопределить метод __missing__, который вызывается в базовой реализации методом dict.__getitem__ при при адресации к отсутствующему ключу.

Мы можем, например, возвращать какое-то дефолтное значение, либо определить любую другую необходимую логику поведения.

#dict

BY Python Academy




Share with your friend now:
tg-me.com/python_academy/675

View MORE
Open in Telegram


Python Academy Telegram | DID YOU KNOW?

Date: |

To pay the bills, Mr. Durov is issuing investors $1 billion to $1.5 billion of company debt, with the promise of discounted equity if the company eventually goes public, the people briefed on the plans said. He has also announced plans to start selling ads in public Telegram channels as soon as later this year, as well as offering other premium services for businesses and users.

The STAR Market, as is implied by the name, is heavily geared toward smaller innovative tech companies, in particular those engaged in strategically important fields, such as biopharmaceuticals, 5G technology, semiconductors, and new energy. The STAR Market currently has 340 listed securities. The STAR Market is seen as important for China’s high-tech and emerging industries, providing a space for smaller companies to raise capital in China. This is especially significant for technology companies that may be viewed with suspicion on overseas stock exchanges.

Python Academy from vn


Telegram Python Academy
FROM USA