[Python-ideas] making a module callable

Oleg Broytman phd at phdru.name
Tue Nov 26 02:37:42 CET 2013


Hi!

On Tue, Nov 26, 2013 at 12:43:25PM +1300, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Ben Finney wrote:
> >Automatically discarding the first argument, on the assumption that the
> >program doesn't care about it, is making a false assumption in many
> >cases and makes a common use case needlessly difficult.
> 
> If you're talking about doing different things based on
> argv[0], I wouldn't call it a *common* use case. The last
> time I saw it done was on an early version of SunOS that
> didn't have shared libraries, so they linked all the gui
> tools into one big executable to reduce disk and memory
> usage.
> 
> Now that we have shared libraries, there's much less
> need for that kind of trick.

   sys.argv[0] is used for:

1) Setup sys.path; something like
lib_dir = os.path.dirname(sys.argv[0]) + os.sep + 'lib')
sys.path.append(lib_dir)

2) Setup relative path(s) (to start a helper script, e.g.); something
like
Subprocess("%s/Robots/sub.py" % os.path.dirname(sys.argv[0]))

3) Report usage:
sys.stderr.write("Usage: %s [-o|--old]\n" % sys.argv[0])

4) Change behaviour based on the script's name.

   I am -1 on removing sys.argv[0] from main(argv).

Oleg.
-- 
     Oleg Broytman            http://phdru.name/            phd at phdru.name
           Programmers don't die, they just GOSUB without RETURN.


More information about the Python-ideas mailing list