[Tutor] How to type annotate complex dictionary containing lambdas?

Alan Gauld alan.gauld at yahoo.co.uk
Sun Nov 17 03:52:31 EST 2019


On 17/11/2019 05:32, boB Stepp wrote:
> My best effort so far to type annotate the dictionary below is to use
> "Any" for the dictionary values.  Is there a better substitute for
> "Any" that would more precisely type annotate these dictionary values?

To be honest I wouldn't even be trying I'd be more concerned with
getting rid of such a messy dict and replacing it with a class.

Type hints are no substitute for clean code.

> goal_year_params: Dict[str, Any] = {
>           "greeting_msg": "Please enter the date by which you wish to attain"
>           " your goal.\n",
>           "input_prompt": "Enter year of your goal as an integer:  ",
>           "date_value_err_ck": lambda goal_year: date(goal_year, 1, 1),
>           "err_msg": "That is not a valid year.  Please try again.",
>           "conditions": [
>               (
>                   lambda goal_year: goal_year < date.today().year,
>                   lambda goal_year: print(
>                       "Have you invented a time machine?  If not,
> please enter a"
>                       " year that makes more sense!"
>                   ),
>               ),
>               (
>                   lambda goal_year: goal_year >= date.today().year + 100,
>                   lambda goal_year: print(
>                       "Have you discovered the secret to eternal life?
>  And how"
>                       " long is this book anyway?  Please enter a year that"
>                       " makes more sense."
>                   ),
>               ),
>           ],
>       }


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list