[Tutor] NEWBIE!! pipes

Michael P. Reilly arcege@shore.net
Thu, 1 Feb 2001 13:29:01 -0500 (EST)


> What are pipes?  The docs barely touched this subject, it assumed you already 
> knew what they are...  From context I am gathering that it is a connection to 
> the kernel?  (maybe?)  How can I utilize this?

Pipes are a feature of the operating system, not of Python; and then,
mostly in POSIX operating systems.  They are often used to connect the
output of one process to the input of another.  For example on MS-DOG,
it is common to run `type autoexec.bat | more' to pause listing the
file after every screenful, the "|" is the pipe character.  Refer to a
lot of shell scripting or UNIX system programming books for different
ways to utilize this fairly powerful construct.

> 1 more quickie:
> >if __name__ == '__main__':
> >     main()
> 
> what is the pourpose if the above code?  It runs the sub main() (duh :) but, 
> why not just main() wihtout the if?  what is the conditional testing for?  
> and what are the variables __main__ and __name__?  i dont believe they are 
> defined in the program, so just what are they?  and, what does putting "__" 
> around a variable actually DO besides look cool ?

This is explained in the Python FAQ 4.10 "How do I find out whether I
am running as a script?"  Each module has a variable called
"__name__".  If variable contains the name "__main__", then the module
is what was called from the command line (the "script"), instead of
imported from another module.

$ cat foo.py
if __name__ == '__main__':
    print 'script'
else:
    print 'imported'
$ python foo.py
script
$ python
>>> import foo
imported
>>>

Most often it is used to test a module meant to be imported, so only if
you execute it directly, it runs the testing code.

  -Arcege

-- 
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |
------------------------------------------------------------------------