Symbols as parameters?

Alf P. Steinbach alfps at start.no
Thu Jan 21 17:49:59 EST 2010


* Martin Drautzburg:
> Thanks for all the answers. Let me summarize
> 
> (1) I fail to see the relevance of 
>  >>> def move( direction ):
> ...   print( "move " + str( direction ) )
> ...
>  >>> move( "up" )
> move up
> 
> not only in the context of my question. And I don't see an abuse of the
> language either. Maybe this could pass as a Zen Puzzle.

Oh. You focused on the wrong details, then. Sorry for not providing a textual 
explanation, but I thought anyone not understanding it would just ask (that's 
normal in other Usenet groups that I'm familiar with).

So, explanation...

Here's your problem description, from the start of the thread:

<quote>
What I am really looking for is a way

         - to be able to call move(up)
         - having the "up" symbol only in the context of the function call

So it should look something like this

... magic, magic ...
move(up)
... unmagic, unmagic ...
print up

This should complain that "up" is not defined during the "print" call,
but not when move() is called. And of course there should be as little
magic as possible.
</quote>


The code corresponding to your first "magic, magic ..." is

     class using_directions:
         # Whatever statement (import?) defines symbolic directions goes here

Then comes your  --  note the added indentation

         move( up )

Your "... unmagic, unmagic" is simply to go back to the previous indent level.

So your example's following

     print up

at this point complaints that "up" is not defined, as you require (no general 
namespace pollution, although there is a slight but controllable pollution, 
namely the class name).

Not that I recommend this technique. ;-) But it does exactly what you asked, 
quoted above.

The best technique in my view is what you came up with yourself in the article 
I'm responding to (but snipped by me), namely a class with the constants and the 
function. It doesn't do what you required originally. But it's much better! :-)


Cheers & hth.,

- Alf



More information about the Python-list mailing list