Symbols as parameters?
Diez B. Roggisch
deets at nospam.web.de
Fri Jan 22 00:16:31 CET 2010
Am 21.01.10 22:51, schrieb 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.
> (2) Using enum's was suggested. That is good to know, but again it is
> just a way to define constants in the caller's namespace.
> (3) Then somone suggested to tie the constants to the function itself,
> as in
> def move(direction):
> print "moving %s" % direction
> move.UP = 'up'
> move.DOWN = 'down'
> This is quite nice. Then again the "move." is just some object which
> allows attributes, and which only happens to have the same name as the
> function. Well in this case it IS the function, alright, but I could
> just as well have used a Class as in
> class m: pass
> m.UP = 'up'
> (4) Finally someone mentioned DSLs. I guess thats absolutely correct.
> This is what I am struggeling to achieve. I did a little googling ("how
> to write DSLs in python"), but haven't found anything appealing yet.
> Any pointers would be appreciated.
> (5) Here is something I came up with myself:
> def symbols(aDict):
> aDict["foo"] = "bar"
> def someFunction(aFoo):
> print aFoo
> someFunction (foo) #Eh voila: foo is magically defined
If it works, it's more an accident than anything else, and can go away
without prior notice.
More information about the Python-list