Seeing "locals()" from imported function

L

Luis M. González

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?

Luis
 
7

7stud

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.
 
7

7stud

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)
 
L

Luis M. González

This is one of these times when I feel so dumb and ashamed that I
wished I never dared to ask...
Well, that did the trick. Thanks!

luis
 
S

Steve Holden

Luis said:
This is one of these times when I feel so dumb and ashamed that I
wished I never dared to ask...
Well, that did the trick. Thanks!

luis
No need. You are allowed to learn.

If you come back with the same question ina week's time, *then* you can
expect trouble ;-)

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top