Skip to main content

Add more constant "None" objects to Python to avoid boilerplate code

Project description

python-none-objects

pypi-version

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_none_objects-1.1.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_none_objects-1.1.1-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file python_none_objects-1.1.1.tar.gz.

File metadata

  • Download URL: python_none_objects-1.1.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for python_none_objects-1.1.1.tar.gz
Algorithm Hash digest
SHA256 b25e4fbc3c9f8f4c58f6cdf362777c0117620d6bbcb82e5080d779f4013625b7
MD5 8683b06870099db0e9673d211767207f
BLAKE2b-256 35e4b41d05551615408bc4db5ac7befbc9099b3149bce298ac2b7f94f4eb5b7b

See more details on using hashes here.

File details

Details for the file python_none_objects-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_none_objects-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1459803b253c640c299ad9ce2cb8630efcf75b071cb3f7b3ac4a168074cddab3
MD5 cbb14bde95d511e33b6d274f6ce7daa5
BLAKE2b-256 ae60ef0461bba9fba32024ff4d52fe9768479434da1fe36f64ba1efa5ba4d610

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page