A dictionary and counter package that only allows a single NaN instance key
Project description
This is not the NaN you are looking for
Alternate title: "NaN - More than Not a Number"
- Python oddities concerning NaN
- Live demo a dict that allows use of NaN as a key the way it would be intuitively, so that all NaN values are treated as one.
- Live demo a Counter that counts how many occurences of NaN exist
Questions
- What about
copy.copy?
Description
Have you ever wondered about float('NaN')? What it is? What you can do with it? What you
shouldn't do with it? Let's take some time and explore this Not a Number and what it does
(and doesn't do) in Python.
Outline
- Introduction. 3 minutes
- Who am I?
- What am I going to talk about?
- What led me to write this talk?
- Introducing
NaN. 3 minutes- What is
NaN? - What purpose does
NaNserve? - The differences beteween
float('nan'),numpy.nan, andmath.nan. - How we can tell if a "numeric" value is NaN.
- What is
- What makes
NaNso unique? 5 minutes- No 2 instances of
NaNare the same. - All instances of
NaNhave the samerepr: "nan" NaNis not equal to anything, not even itself.NaNcan be hashed but each instance ofNaNwill have a different hash.- A set can contain multiple instances of
NaN. - A dictionary can have multiple
NaNkeys, but good luck accessing the data. - If you have an
Iterablewith multiple instances ofNaNand create aCounterfrom it, each instance will appear with a count of 1.
- A set can contain multiple instances of
- No 2 instances of
- Creating a Pythonic
dictthat can only have oneNaN. 12 minutes- Do a code walkthrough and live demo of a Python subclass of
dictthat can only have one instance ofNaN.- Show how I created the new version of
dict. - Simple live demo showing that we can use
float('nan')as a dict key. In a regulardictit will give aKeyErrorwhen attempting to retrieve it; with the new version ofdictit will work as expected.
- Show how I created the new version of
- Do a code walkthrough and live demo of a Python
Counterclass that will count how many instances ofNaNare in it.- Show how I created a
Counterclass that will count all instances ofNaN. - Live demo showing the behaviour of
collections.Counterand the newCounterclass.
- Show how I created a
- Do a code walkthrough and live demo of a Python subclass of
- Conclusion. 2 minutes
- Quick recap of what was discussed.
- Where to find the talk slides.
- Where to find me.
Things I've found
>>> float('nan') ** 0
1.0
>>> float('nan') > 1
False
>>> float('nan') < 1
False
>>> float('nan') == 1
False
>>> float('nan') != 1
True
>>>> float('inf') - float('inf')
nan
>>>> f'ba{1e234567 - 1e765432}as'
'bananas'
import struct
float_bytes = b"\xaa\xbb\xcc\xdd\xff\xff\xff\xff"
val = struct.unpack("d", float_bytes)[0]
print(val)
print(struct.pack("d", val))
In a single precision floating point number (32 bit) there are 2^24 - 2 potential NaN values.
Potential improvements for NaNDict:
- Different types of nans are distinct.
- Perserve the original supplied nan (maybe you do and check_key() does the coalescing).
- A dunder hook for hypothetical future other weird ass values with rogue behaviour like nan.
- An extensive test and performance suite.
- Accept any kind of object for the membership test.
- Accepting unhashable values.
Quotes
NaN is a lot like infinity in that people want to treat it as a number but It's Not and trying to do that will only lead to surprise and disappointment. - Luna Celste
NaN is a wild land governed by laws both alien and arcane - Luna Celeste
References
https://brassnet.biz/blog/nan-is-weird.html
https://github.com/python/cpython/issues/87641
https://twit.social/@brass75/116168712645625711
https://discuss.python.org/t/question-about-float-nan/106378
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nandict-2026.6.2.tar.gz.
File metadata
- Download URL: nandict-2026.6.2.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fe99b0e51e1af7f597464052a1755ce5945b66727168ed42b222744a3a4ba17
|
|
| MD5 |
fe52f5fdc3f39826b2b5acb3add269b6
|
|
| BLAKE2b-256 |
c7465f3b651f4b18ba7a3b1555e0bcd093a39d9ff982128b3f528e965099afad
|
Provenance
The following attestation bundles were made for nandict-2026.6.2.tar.gz:
Publisher:
publish.yml on brass75/nan-talk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nandict-2026.6.2.tar.gz -
Subject digest:
9fe99b0e51e1af7f597464052a1755ce5945b66727168ed42b222744a3a4ba17 - Sigstore transparency entry: 1871106996
- Sigstore integration time:
-
Permalink:
brass75/nan-talk@b191227ddc941e0ee352d9d89fa6114229ebd55c -
Branch / Tag:
refs/tags/2026.6.2 - Owner: https://github.com/brass75
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b191227ddc941e0ee352d9d89fa6114229ebd55c -
Trigger Event:
release
-
Statement type:
File details
Details for the file nandict-2026.6.2-py3-none-any.whl.
File metadata
- Download URL: nandict-2026.6.2-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
765df2892e126811ebacd4d5b6dc4d206d926d11c4d9af6c80aa0c84d50c6c55
|
|
| MD5 |
f0763552eb7846ba7db832c3f7681c90
|
|
| BLAKE2b-256 |
865333d5b68736577792064148adadbc62cace6294fa97d7027635dc31f83012
|
Provenance
The following attestation bundles were made for nandict-2026.6.2-py3-none-any.whl:
Publisher:
publish.yml on brass75/nan-talk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nandict-2026.6.2-py3-none-any.whl -
Subject digest:
765df2892e126811ebacd4d5b6dc4d206d926d11c4d9af6c80aa0c84d50c6c55 - Sigstore transparency entry: 1871107078
- Sigstore integration time:
-
Permalink:
brass75/nan-talk@b191227ddc941e0ee352d9d89fa6114229ebd55c -
Branch / Tag:
refs/tags/2026.6.2 - Owner: https://github.com/brass75
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b191227ddc941e0ee352d9d89fa6114229ebd55c -
Trigger Event:
release
-
Statement type: