Skip to content

Instantly share code, notes, and snippets.

@hseritt
Created April 15, 2023 10:59
Show Gist options
  • Save hseritt/8a7e06b8dd2a15149fde99e751976b55 to your computer and use it in GitHub Desktop.
Save hseritt/8a7e06b8dd2a15149fde99e751976b55 to your computer and use it in GitHub Desktop.
Using namedtuple for constants in python
from collections import namedtuple
Socket = namedtuple("Socket", ["DEFAULT_TIMEOUT"])
# Set a default timeout for sockets
SOCKET = Socket(DEFAULT_TIMEOUT=10)
print(SOCKET.DEFAULT_TIMEOUT)
try:
SOCKET.DEFAULT_TIMEOUT = 40
except AttributeError:
print "This won't work"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment