[Tutor] python game error

Mats Wichmann mats at wichmann.us
Sun Oct 14 13:42:39 EDT 2018


On 10/14/2018 10:08 AM, bob gailer wrote:

> ------------ Python coding "trick"1  ------------
> when I build a map I omit the () after the class e.g. 'death' = Death,
> ... and apply them to the item retrieved from the map.
> 
> use a decorator to build the map dictionary:
> 
> # the decorator function:
> def add_to_map(cls, map={}): # map is initialized to a {} when function
> is "compiled"
>     if cls:
>         map[cls.__name__] = cls # add entry to dictionary
>         return cls
>     else: return map

Hint here: don't use 'map' as your own variable name, since it's a
built-in function. It's not that it won't work, but is likely to cause
confusion someday. Most "code inspection" systems will complain that you
are shadowing or redefining a builtin (terminology may vary).  The list
of builtins - thus names to avoid - is here:

https://docs.python.org/3/library/functions.html


More information about the Tutor mailing list