Faking out __name__ == __main__
Bengt Richter
bokr at oz.net
Mon Nov 1 14:45:25 EST 2004
On Mon, 1 Nov 2004 10:06:09 +0200, aleaxit at yahoo.com (Alex Martelli) wrote:
>Josiah Carlson <jcarlson at uci.edu> wrote:
>
>> bokr at oz.net (Bengt Richter) wrote:
>> >
>> > On Sun, 31 Oct 2004 17:05:59 -0500, Ed Leafe <ed at leafe.com> wrote:
>> > >>> def test():
>> > ... d = {'__name__': '__xxxx__'}
>> > ... execfile('tmain.py', d)
>>
>> I believe it is technically more Pythonic to use __import__ rather than
>> execfile, if only because you have access to the module itself when you
>> are done.
>
>Unfortunately, while __import__ does take parameters giving local and
>global namespaces, it doesn't use them in anywhere like the way that
>execfile does. In particular, how would you "force __name__" with
>__import__, as per this thread's subj? I don't think you can...
>
It seems from a quick hack that you can fake it though, by importing a dummy
and filling it:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print '%s\n%s%s' %('-'*40,open('empty.py').read(),'-'*40)
----------------------------------------
----------------------------------------
>>> print '%s\n%s%s' %('-'*40,open('tmain.py').read(),'-'*40)
----------------------------------------
print 'before'
print 'bef localnames:', locals().keys(), locals()['__name__']
print 'bef globalnames:', globals().keys(), globals()['__name__']
glob_before = 'glob_before'
class Before(object):
def m(self): print glob_before, glob_after
if __name__ == '__main__':
glob_after = 'glob_after'
class After(object):
def m(self): print glob_before, glob_after
print 'after'
print 'aft localnames:', locals().keys(), locals()['__name__']
print 'aft globalnames:', globals().keys(), globals()['__name__']
----------------------------------------
>>> import empty
>>> empty.__dict__['__name__']
'empty'
>>> empty.__dict__['__name__'] = '__main__'
>>> empty.__dict__['__name__']
'__main__'
>>> execfile('tmain.py', empty.__dict__)
before
bef localnames: ['__builtins__', '__name__', '__file__', '__doc__'] __main__
bef globalnames: ['__builtins__', '__name__', '__file__', '__doc__'] __main__
after
aft localnames: ['glob_before', '__builtins__', '__file__', 'After', 'glob_after', '__name__', '
__doc__', 'Before'] __main__
aft globalnames: ['glob_before', '__builtins__', '__file__', 'After', 'glob_after', '__name__',
'__doc__', 'Before'] __main__
>>> dir(empty)
['After', 'Before', '__builtins__', '__doc__', '__file__', '__name__', 'glob_after', 'glob_before']
>>> empty.Before().m()
glob_before glob_after
>>> empty.After().m()
glob_before glob_after
>>> empty.glob_before
'glob_before'
>>> empty.__name__='tmain'
>>> empty.__file__ = r'c:\pywk\clp\tmain.py'
>>>
>>> help(empty)
Help on module tmain:
NAME
tmain
FILE
c:\pywk\clp\tmain.py
DATA
glob_after = 'glob_after'
glob_before = 'glob_before'
>>> type(empty)
<type 'module'>
I haven't really explored all the ramifications ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list