Here's just a real life example from one my Django projects. It gives me the ability to essentially 'loop' through my class, and each time it loops, it's going to grab the most up-to-date value for the variable it's accessing. I also love being able to intuitively loop through a class, and yield is key to implementing a custom iteration.
class AllowedValues:
class BaseAllowedValues:
def __init__(self):
self.DEFAULT = None
def items(self):
return {self.DEFAULT}
def __repr__(self):
return str(self.items())
def __iter__(self):
for value in self.items():
yield value
class BooleanTypes(BaseAllowedValues):
def __init__(self):
super().__init__()
self.TRUE = True
self.FALSE = False
self.DEFAULT = False
def items(self):
return {self.DEFAULT, self.TRUE, self.FALSE}
class ToolTipTypes(BaseAllowedValues):
def __init__(self):
super().__init__()
self.BODY = 'body'
self.HEADER = 'header'
self.DEFAULT = ''
def items(self):
return {self.DEFAULT, self.BODY, self.HEADER}