Big file

Simon Forman sajmikins at gmail.com
Thu Mar 13 01:59:33 CET 2008


On Mar 12, 5:42 pm, "Andrew Rekdal" <as... at comcast.net> 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?
>
> Thanks -- andrew

First, you should consider breaking your __init__() method into
smaller pieces (methods) and calling those from within __init__().

That said, you can add attributes to an instance by means of its
__dict__ dict attribute:

|>> class foo:
|>>     def __init__(self):
|>>         self.__dict__['hi'] = ['An object']
|>>         print self.__dict__
|>>

|>> f = foo()
{'hi': ['An object']}

|>> f.hi
['An object']


You might try:

|>> exec 'from sys import *' in f.__dict__

Now everything in sys appears in f:

|>> f.copyright
'Copyright (c) 2001-2006 Python Software Foundation.\nAll Rights
Reserved.\n\nCopyright (c) 2000 BeOpen.com.\nAll Rights Reserved.\n
\nCopyright (c) 1995-2001 Corporation for National Research
Initiatives.\nAll Rights Reserved.\n\nCopyright (c) 1991-1995
Stichting Mathematisch Centrum, Amsterdam.\nAll Rights Reserved.'

HTH,
~Simon



More information about the Python-list mailing list