How to write Inline Functions in Python?

Arivazhagan arivu at qmaxtest.com
Thu Nov 14 23:04:59 EST 2002


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


Peter Hansen <peter at engcorp.com> wrote in message news:<3DD3C06D.61AAB36D at engcorp.com>...
> Arivazhagan wrote:
> > 
> > Hi Tim
> > 
> > I assume you're coming from C++. - Thats true.
> > 
> > Hi
> > 
> > My requirements here.
> > 
> > we have three python files names a.py, b.py and c.py.
> > In a.py we are calling a global function in c.py which creates objects
> > of a class in c.py depending on the parameters passed. These objects
> > are created in the scope of c.py. But i need to happen in a.py without
> > using the module name.
> > so I thought that if we inline the function call so that it expands we
> > can access that object without referring to the module name.
> > 
> > Is it possible? Is there some other methods to achieve this in python?
> 
> Certainly there is, but you haven't explained clearly enough what you
> really want.  It sounds like you could simply be returning those objects
> from a function defined in c.py and called from a.py.
> 
> My guess is you are mucking around with global variables at module 
> scope level and getting yourself into a lot of trouble.
> 
> Can you explain *why* you need to refer to objects defined in another
> module without referring to that module's name?  Or can you explain
> why these objects are being created as globals instead of just 
> returning them from a function?
> 
> -Peter



More information about the Python-list mailing list