Seeing "locals()" from imported function

7stud bbxx789_05ss at yahoo.com
Sun Feb 24 07:49:35 CET 2008


On Feb 23, 11:41 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
> On Feb 23, 10:06 pm, "Luis M. González" <luis... at gmail.com> wrote:
>
>
>
> > I apologize for this very basic question, but I can't understand how
> > this works...
> > I want to import a function from module B into my main script A, so
> > this function can see and use the locals from A.
>
> > For example:
>
> > def auto():
> >     urls = ['/', 'index']
> >     for k,v in __main__.locals().items():         #  these "locals"
> > are the ones of the main script
> >             if isinstance(v,type) and k != 'index':
> >                     urls.append('/%s' %k)
> >                     urls.append(k)
> >     return tuple(urls)
>
> > Of course this doesn't work...
>
> > Any hint?
>
> Yes, define your functions so that they get all the input they need
> from the arguments that are passed in.

For instance:

#file1.py:
def auto(a_list):
    for elmt in a_list:
        print elmt

#file2.py:
import file1

file1.auto(whatever)



More information about the Python-list mailing list