How to write Inline Functions in Python?

David Brown david at no.westcontrol.spam.com
Fri Nov 15 03:31:12 EST 2002


"Arivazhagan" <arivu at qmaxtest.com> wrote in message
news:c8476101.0211142004.7f97751b at posting.google.com...
> Hi
> We are extending python to develop a Board Description language for
> ATE. say in our project there are 2 modules named a.py( provided by us
> ) and b.py.(the end user programs.)
>
> In a.py
> -------
>
> import sys
>
>
> class Good :
>     def __init__(self, name ) :
>         self.__name = name
>
>
> def createGood( *names ) :
>     for name in names :
>        thisGood = Good( name )
>
> #The following is done so that the objects created here can be
> accessed
> #from b.py without using the module name.
> #We just want to avoid this.
>        sys.modules['__main__'].__dict__[name] = thisGood
>
>
> In b.py
> -------
> from a import *
>
> createGood( 'A', 'B', 'C' )
>
> #We want to access like this without that additional codes.
> print A
> print B
> print C
>
> regards
> b.arivazhagan
>
>

As far as I can see (and hopefully a more experianced Python programmer will
correct me if I'm wrong), you are definitely going about this the wrong way.
You do not want to create globals in this manner - if writing "print x.A,
x.B, x.C" is going to be a problem, then you are going to have to go back
and think about what you are really trying to do.  Sure, it would be
possible to mess around with globals() and get the effect you want somehow,
but it really would be messing around.  It would also be incredibly
dangerous - think what would happen with " createGood('createGood', 'a',
'print') ".

I think you would be much better off having createGood make a list or a
dictionary of the Good objects and returning that.







More information about the Python-list mailing list