Skip to main content

To avoid NoneType AttributeError exception on chained attributes

Project description

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

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

noattr-0.0.9.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

noattr-0.0.9-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file noattr-0.0.9.tar.gz.

File metadata

  • Download URL: noattr-0.0.9.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for noattr-0.0.9.tar.gz
Algorithm Hash digest
SHA256 1d65d4134327fa3f921bdae02326c8784383c4262864e2f5e57dca6ae1999b4b
MD5 5ce27264964be8b6eca226e672cffb85
BLAKE2b-256 2d5a67861635b4331f2192438741232e8ac7e3b69b0a4cc1bd352d52c2544413

See more details on using hashes here.

File details

Details for the file noattr-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: noattr-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for noattr-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 75660b845a4727982529764f02133f688ac5ca01d1469e696fea7b4901dc1904
MD5 a0cee82752008e5c85a4bc35e0e13ef7
BLAKE2b-256 ad095398c96c755aefbef6b3d09ef88b79fbbc4131021095343ea7e0c2a7e3c2

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