Creating a Dictionary

Bill BILL_NOSPAM at whoknows.net
Wed Oct 4 22:02:50 EDT 2017


Stefan Ram wrote:
>    One might wish to implement a small language with these commands:
Explain why. What is the advantage?


>
> F - move forward
> B - move backward
> L - larger stepsize
> S - smaller stepsize
>
>    . One could start with the following pseudocode for a dictionary:
>
> { 'F': lambda: myturtle.forward( s ),
>    'B': lambda: myturtle.backward( s ),
>    'L': lambda: global s; s *= 2,
>    'S': lambda: global s; s /= 2 }
>
>    . But lambda-expressions cannot contain statements.
>
>    In real Python one could write something like (untested):
>
> def f():
>      myturtle.forward( s )
>
> def b():
>      myturtle.backward( s )
>
> def l():
>      global siz
>      size *= 2
>
> def s():
>      global siz
>      size /= 2
>
> { 'F': f,
>    'B': b,
>    'L': l,
>    'S': s }
>
>    . Is this more readable or less readable?
>
>    Any other suggestions?
>




More information about the Python-list mailing list