Here's a more fleshed-out example of the kind of usage I have in mind. Note
that this does not require `with` to have any affect on variable scope. The
object returned from the context manager's __enter__ method is all the context
that is needed or wanted for this pattern.
restaurant_choices = with build_choices('Restaurant') as cb:
# Get of nonexistent attr auto-generates matching entry.
cb.BURGER_KING
cb.FIVE_GUYS
# Returned value is a ChoiceItem that implements __mod__ to return a
# new ChoiceItem instance w/ substituted label.
cb.MCDONALDS %= "McDonald's"
print(restaurant_choices)
# RestaurantChoices(
# (
# ('BURGER_KING', 'Burger King'),
# ('FIVE_GUYS', 'Five Guys'),
# ('MCDONALDS', "McDonald's")))
print(RestaurantChoices.BURGER_KING)
# Prints 'BURGER_KING'
print(RestaurantChoices.value_labels['FIVE_GUYS'])
# Prints 'Five Guys'
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ZPZ47KHN7JWWCWF7UH4P2MYLK6XTRI6O/
Code of Conduct: http://python.org/psf/codeofconduct/