[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Seeing "locals()" from imported function

Luis M. González

2/24/2008 5:06:00 AM

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
4 Answers

7stud --

2/24/2008 6:42:00 AM

0

On Feb 23, 10:06 pm, "Luis M. González" <luis...@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.

7stud --

2/24/2008 6:50:00 AM

0

On Feb 23, 11:41 pm, 7stud <bbxx789_0...@yahoo.com> wrote:
> On Feb 23, 10:06 pm, "Luis M. González" <luis...@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)

Luis M. González

2/24/2008 3:24:00 PM

0

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

On Feb 24, 3:41 am, 7stud <bbxx789_0...@yahoo.com> wrote:
> Yes, define your functions so that they get all the input they need
> from the arguments that are passed in.

Steve Holden

2/24/2008 3:56:00 PM

0

Luis M. González wrote:
> 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
>
> On Feb 24, 3:41 am, 7stud <bbxx789_0...@yahoo.com> wrote:
>> Yes, define your functions so that they get all the input they need
>> from the arguments that are passed in.
>
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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...