How to get the package, file, and line of a method/function invocant?

metaperl metaperl at gmail.com
Tue Sep 12 01:21:17 EDT 2006


I am looking for something like the caller() routine in Perl:
   http://perldoc.perl.org/functions/caller.html

I am writing a script which needs to be allocated an object containing
a set of paths that it will use for reading and writing during it's
execution:

import os.path

class logic:
    def __init__(self):
        std_dirs = "in out zip arc".split()
        self.root = os.path.dirname(__main__.__file__) # doesnt work
        for d in std_dirs:
            mkdir = "%s/%s" % (self.root, d)
            os.mkdir(mkdir)
            setattr(self, d, mkdir)


# Then the script can do this:

directories = data.storage.logic()
infile = "%s/%s" % (directories.in, "infile")
f = open (infile, 'r')

# Of course I could cheat and pass it, but I don't want to:

directories = data.storage.logic(__file__)




More information about the Python-list mailing list