Last active
March 12, 2026 04:16
-
-
Save pythonhacker/def698626b1af617d7662b51040ccb05 to your computer and use it in GitHub Desktop.
RGB Color class with validation overriding __setattr__
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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