python-none-objects
| A collection of "None" objects |
|---|
| compatible with various Python types |
The following code yields warning for "Default argument value is mutable".
from typing import List, Dict
def foo(some: int, other: List = [], thing: Dict = {}):
for o in other:
bar(some, o, thing)
It is usually recommended to use None instead (https://stackoverflow.com/questions/41686829 /why-does-pycharm-warn-about-mutable-default-arguments- how-can-i-work-around-the):
from typing import List, Dict, Optional
def foo(
some: int,
other: Optional[List] = None,
thing: Optional[Dict] = None,
):
if other is None:
other = []
if thing is None:
thing = {}
for o in other:
bar(some, o, thing)
But I prefer less boilerplate code like this:
from typing import Iterable, Mapping
from types import MappingProxyType
def foo(
some: int,
other: Iterable = (),
thing: Mapping = MappingProxyType({}),
):
for o in other:
bar(some, o, thing)
This package introduces constants to make the code more readable:
from typing import Iterable, Mapping
from python_none_objects import NoneIterable, NoneMapping
def foo(
some: int,
other: Iterable = NoneIterable,
thing: Mapping = NoneMapping,
):
for o in other:
bar(some, o, thing)
Be sure to look at the discussions on GitHub: https://github.com/LLyaudet/python-none-objects/discussions.
There is a poll on the naming convention you would prefer: https://github.com/LLyaudet/python-none-objects/discussions/2.
And there is a discussion on various ideas to optimize the code with these constants: https://github.com/LLyaudet/python-none-objects/discussions/3.
I think it would be better to have this kind of constants in the standard library. If you think after reading everything, that it is indeed a good idea, add a star to this repository to let the rest of the Python community know that you would like to see such constant objects in the language :). https://github.com/LLyaudet/python-none-objects/ If the project gains popularity, I'll try to propose it officially.
Metadata
Release files for python-none-objects 1.1.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_none_objects-1.1.11.tar.gz | 100.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_none_objects-1.1.11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 122.0 kB
Release files / python_none_objects-1.1.11.tar.gz
| Download URL | python_none_objects-1.1.11.tar.gz |
|---|---|
| Size | 100.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7dfad1430f4e413d713c733056db1099d19fd07de1a2e30df24bd2ed2dcb683
|
|
BLAKE2b-256 checksum How to use checksums |
052139fa0e02727e7d1f288ec872ee4d9722bc777c24a21bdc8c90ff21e294dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.6
|
Release files / python_none_objects-1.1.11-py3-none-any.whl
| Download URL | python_none_objects-1.1.11-py3-none-any.whl |
|---|---|
| Size | 21.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
37b90e54a0306c523550d81f422b5892f17bd096c4a1ecd51877a9be4a489c70
|
|
BLAKE2b-256 checksum How to use checksums |
6bc17b495c6983c3de31ff38833a73c2a5999dac0b92a1f1539a7b6b60a5759e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.6
|