Skip to content

Instantly share code, notes, and snippets.

@pythonhacker
Last active March 12, 2026 04:16
Show Gist options
  • Select an option

  • Save pythonhacker/def698626b1af617d7662b51040ccb05 to your computer and use it in GitHub Desktop.

Select an option

Save pythonhacker/def698626b1af617d7662b51040ccb05 to your computer and use it in GitHub Desktop.
RGB Color class with validation overriding __setattr__
class ColorAttr(Color):
""" Color class with attribute validation """
def __init__(self, r=0, g=0, b=0):
self.r = r
self.g = g
self.b = b
def __setattr__(self, name, value):
if name in ("r", "g", "b"):
# Validate type
if type(value) is not int:
raise ValueError("RGB values must be integers")
# Validate range
if not value in range(0, 256):
raise ValueError("RGB values must be in range {0...255}")
super().__setattr__(name, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment