Or if the context object needs different behavior, stick the "return value" in an attribute like restaurant_choices.data.

On Sun, Oct 13, 2019, 2:56 PM David Mertz <mertz@gnosis.cx> wrote:
Why not just:

    with build_choices('Restaurant') as restaurant_choices :
        ...

    print(restaurant_choices)

On Sun, Oct 13, 2019, 2:49 PM Steve Jorgensen <stevej@stevej.name> wrote:
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 -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/ZPZ47KHN7JWWCWF7UH4P2MYLK6XTRI6O/
Code of Conduct: http://python.org/psf/codeofconduct/