[Tutor] does python have something like "#include" in C?

Robert Lummis robert.lummis at gmail.com
Mon Jun 29 19:23:41 CEST 2009


Here's an example that seems not possible in python. I'm probably
missing something so please enlighten me. I only tried doing this as
an exercise to show myself how name references work. I'm not saying
it's needed or that it's good practice.

I can write the following as a single file and it works as expected:

===snip===
#!/usr/bin/python

def show(*args):
    print
    for arg in args:
	    print arg + ':',
	    exec('print ' + arg)

a=15
b='hello'
x=['bob',3]

show('a')
show('a','b')
show('a','b','x')
===snip===

The calls to 'show' output lines like "a: 15" which could be useful
for debugging or some such purpose.

However, it seems that I can't put the function definition in a file
and import it because I can't find a way to refer to an object in the
main program file from within a module file. I understand that it's a
good thing to contol which namespaces are referenced by which code but
isn't there sometimes a need for code in a module to access the main
program file's namespace? My example may be a little contrived but
isn't this ability legitimately needed at times?

On Mon, Jun 29, 2009 at 11:22 AM, A.T.Hofkamp<a.t.hofkamp at tue.nl> wrote:
> Robert Lummis wrote:
>>
>> ... or any pre-processing at all?
>>
>> I'm looking for a way to get boiler plate code into the main program
>> file. Of course I could copy and paste it with an editor but I was
>> hoping for something more pythonic. I know about import but that's not
>> the same.
>
> Python is very good at eliminating boilerplating, so import should be enough
> normally.
>
> Could you give an example of what you want to include that cannot be done
> with import?
>
> Albert
>
>



-- 
Robert Lummis


More information about the Tutor mailing list