Skip to main content

NoAttr

When accessing an object with chained attributes,

info = obj.a.b.c.d or 'Unknown'

naïvely this will fail as soon as one of the intermediate attributes returns None:

>>> obj.a
None

>>> obj.a.b.c.
AttributeError: 'NoneType' object has no attribute 'b'

NoAttr gives you a way around this in cases where a series of None checks would be too much effort and result in complex code. Instead of returning None, you want to return an instance of NoAttr, which will caused hierarchical attribute accesses to keep returning NoAttr until the end of the chain

>>> from noattr import NoAttr
>>> obj = NoAttr

>>> obj.a
NoAttr

>>> obj.a.b.c.d
NoAttr

NoAttr behaves as a “falsy” value, meaning that it can stand for None, False, 0, '', [] or {}, depending on context. Here are some examples:

>>> obj.a
NoAttr

>>> obj.a.b.c.d or 'Unknown'  # behaves like a falsy value
'Unknown'

>>> for i in obj.a.b.c.d:  # behaves like an enumerable that does not yield a value
...     print(i)
(no output)

>>> len(obj.a.b.c.d)  # behaves like an empty collection
0

>>> obj.a.b.c.d + 1  # behaves like a 0
1

>>> obj.a.b.c.d.anyfunc()  # behaves like a callable
NoAttr

However, for ljust(), rjust(), rfind(), find(), rindex(), index(), and count(), NoAttr is seen as a single whitespace character (' ') to preserve the expected behavior of these methods:

>>> obj.a.b.c.d.ljust(3)
'   '

Installation

$ pip install noattr

News

0.0.8 (2018-08-24)

__iter__() now returns an empty iter

0.0.7 (2018-08-21)

Update code for python3 compatibility

0.0.6 (2016-03-11)

remove __or__ and __rshift__ definition for python_textops

0.0.5 (2015-10-13)

add as_list property

0.0.4 (2015-08-19)

add __setattr__ to avoid any modification

0.0.3 (2015-07-27)

First official version

Metadata

Release files for noattr 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for noattr 0.0.9
File Size Uploaded Provenance
noattr-0.0.9.tar.gz 3.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for noattr 0.0.9
File Interpreter ABI Platform
noattr-0.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / noattr-0.0.9.tar.gz

Download URL noattr-0.0.9.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1d65d4134327fa3f921bdae02326c8784383c4262864e2f5e57dca6ae1999b4b
BLAKE2b-256 checksum
How to use checksums
2d5a67861635b4331f2192438741232e8ac7e3b69b0a4cc1bd352d52c2544413
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.9

Release files / noattr-0.0.9-py3-none-any.whl

Download URL noattr-0.0.9-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
75660b845a4727982529764f02133f688ac5ca01d1469e696fea7b4901dc1904
BLAKE2b-256 checksum
How to use checksums
ad095398c96c755aefbef6b3d09ef88b79fbbc4131021095343ea7e0c2a7e3c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.9

Security for noattr 0.0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page