Add more constant "None" objects to Python to avoid boilerplate code
Project description
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)
I think it would be better to have this kind of constants in the standard library. But I'm not good at defending this kind of ideas on the mailing lists and discussions that mostly throw 99 % ideas to the trash. If you want to defend this on python-dev or python-ideas, please do :).
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.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8164dfb0172a59fe25c8ba0a5b9864ed17c1a061476e011b40f651466b36d0ad |
|
MD5 | 633f169696080296b3164f8512a75b7d |
|
BLAKE2b-256 | 39941c6bfd8d6f8cd367db1e1841e26a4433d67a0ed6be38ab0ea24c657e3750 |
Hashes for python_none_objects-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9451dc7fbc8eec031c32791d8cecfac93ae0ba18cff32aa7c465544fba24f746 |
|
MD5 | 408b35f34e7234b96d778ccf7ec52c64 |
|
BLAKE2b-256 | 7c47b8fc72a2b40ebdd30322d5ec864c4c05c09ac9013907bfeff5576371e5e8 |