Add more constant "None" objects to Python to avoid boilerplate code
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python_none_objects-1.1.11.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7dfad1430f4e413d713c733056db1099d19fd07de1a2e30df24bd2ed2dcb683 |
|
MD5 | ffadd7dbeb0c570b6b46142cb92fa8ef |
|
BLAKE2b-256 | 052139fa0e02727e7d1f288ec872ee4d9722bc777c24a21bdc8c90ff21e294dd |
Hashes for python_none_objects-1.1.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37b90e54a0306c523550d81f422b5892f17bd096c4a1ecd51877a9be4a489c70 |
|
MD5 | 673bc6a1c7051e4591a75bbd117b2ec9 |
|
BLAKE2b-256 | 6bc17b495c6983c3de31ff38833a73c2a5999dac0b92a1f1539a7b6b60a5759e |