Symbols as parameters?

Carl Banks pavlovevidence at gmail.com
Thu Jan 21 18:19:58 EST 2010


On Jan 21, 1:51 pm, Martin Drautzburg <Martin.Drautzb... at web.de>
wrote:
> Thanks for all the answers. Let me summarize
[snip]
> (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.

It'll at least corral the symbols you want.


[snip]
> (4) Finally someone mentioned DSLs. I guess thats absolutely correct.
> This is what I am struggeling to achieve.

I see.  Well, Python is a poor choice for defining an internal DSL
(i.e., DSL using the general language's syntax), because it's
(deliberately) rigid in both grammar and semantics.  Soon as you want
to do something a little different you're out of luck.

Python is somewhat better suited for external DSLs.

> I did a little googling ("how
> to write DSLs in python"), but haven't found anything appealing yet.
> Any pointers would be appreciated.

Paul McGuire should be by to recommend PyParsing shortly.

It's not really hard to write a simple DSL if you know how.  It is
pretty hard to learn how, though.  Tools like PyParsing help a lot.


> (5) Here is something I came up with myself:
>
> def symbols(aDict):
>     aDict["foo"] = "bar"
>
> def someFunction(aFoo):
>     print aFoo
>
> symbols(locals())
> someFunction (foo) #Eh voila: foo is magically defined
>
> prints: bar
>
> The call to symbols(locals()) is the "magic, magic" I supected would be
> required in my original posting. If someFunction was a member of a
> class, the symbols would be properly tied to that class (albeit not the
> individual function), but still good enough. I suppose I could wrap it
> in a decorator, which would also do the "unmagic".
>
> In any case, getting the context right seems to be the biggest problem.
> If I don't want to pollute my namespace, those symbols need to be
> defined in some context but undefined in others. AFAIK there are not
> really "blocks" in python and lexical scoping is present primarily in
> functions.

That's pretty much the issue.

BTW, I'm sorry that the thread got a little flamey there.


Carl Banks



More information about the Python-list mailing list