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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2b954662f69f20175fdb75028c4c0c87da13f1b23f26d19ab0e97f551a5bcf0 |
|
MD5 | b41aa1dd9a2d6d6c8eafd7936865789f |
|
BLAKE2b-256 | b21947ab81d2b4cefaa26148e7d068425bc84257be3e940d0148de9561152085 |
Hashes for python_none_objects-1.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | caa755bd2144f75d5162f2b56b4c1f0434cd66ab7d83b7a58c87501563d7ef14 |
|
MD5 | d2ac6fc6a195e8f18869c9a465ce4ff2 |
|
BLAKE2b-256 | 79648b44c5380a4b6cc0110a165cb9a2d171039f7a6fd2266a2d2c9f1c66c451 |