[Tutor] get a module's own (top_level) dict?

Kent Johnson kent37 at tds.net
Sun Nov 9 22:54:36 CET 2008

On Sun, Nov 9, 2008 at 3:42 PM, spir <denis.spir at free.fr> wrote:
> Thank you Lie & Kent, that's it. Excuse me, Lie, I answered too fast.
> Now, to answer Kent questions, there are several reasons why I wish to do
> that.
> These objects will be of a 'pattern' type that (unlike function, for
> instance), don't know how they're called. They need to hold their name to
> pass it to further objects (say, tokens) that will be generated according to
> these patterns, but who are not instances of the patterns. Actually, the
> pattern's name is a kind of token 'type'. You see what I mean?

No, not at all. Why can't you assign the pattern's name when you create it?

> Concretely, I need to instanciate object with a type specified by the
> pattern's name and init data given by the result of the parsing.

This can be done with a dict mapping pattern names to types. I don't
see how having a pattern know its own name helps here.

For example:
In [22]: class Foo:
   ....:     def __init__(self, x):
   ....:         print "Foo(%s)" % x

In [24]: class Bar:
   ....:     def __init__(self, x):
   ....:         print "Bar(%s)" % x

In [25]: types = dict(foo=Foo, bar=Bar)

In [26]: types['foo'](3)

In [27]: types['bar'](42)

> Also, I want to write the pattern --> name --> type --> object toolset in a
> general to be able to reuse it. Also, simply because I want it so!
> There will be many such names, too.
> Also: the patterns will actually be generated at runtime according to a
> config file -- and can also change at runtime (--> reload() grammar module),
> following user customization. So that I don't even know the names at design
> time.

How are you going to create all these named objects?


More information about the Tutor mailing list