#!/usr/bin/python # -*- Mode: Python -*- # vi:si:et:sw=4:sts=4:ts=4 # this program loads 'file.py' and creates an instance of class 'Two' def in_namespace(data): namespace = {} exec(data, globals(), namespace) klass = namespace.get('Two') atwo = klass() return atwo def in_global(data): # create class by exec'ing in global namespace, making One available there exec(data, globals()) atwo = Two() return atwo file = 'file.py' data = open(file).read() print "using namespace method" atwo = in_namespace(data) print "an instance of Two: ", atwo print "member 'one' of the instance: ", atwo.one print "using global method" atwo = in_global(data) print "an instance of Two: ", atwo print "member 'one' of the instance: ", atwo.one