Big file

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Mar 13 02:34:17 CET 2008


On Wed, 12 Mar 2008 19:42:44 -0500, Andrew Rekdal wrote:

> I am working in the class constructor defining elements of an
> application. The problem is the file is getting unmanageble and I am
> wanting to extend the contructor __init__ to another file.
> 
> Is it possible to import directly into the contructor the contents of
> another module file?
> 
> If so how would this be done?


Here's the way you do what you literally asked for:

class MyClass(object):
    def __init__(self, *args):
        # Warning: completely untested
        execfile('myfile.py')  # may need extra arguments?

but you almost certainly don't want to do that. A better way is by 
importing modules, the same as you would for anything else:

class MyClass(object):
    def __init__(self, *args):
        from AnotherModule import constructor
        constructor(self, *args)


But frankly if you find yourself needing to do this because your file is 
"too big" and is unmanageable, I think you are in desperate need of 
refactoring your code to make if more manageable. Pushing vast amounts of 
random code out into other files just increases the complexity: not only 
do you have vast amounts of code, but you have large numbers of files to 
manage as well.




-- 
Steven






More information about the Python-list mailing list