[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Andrew Rekdal

3/13/2008 12:43:00 AM

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
5 Answers

Simon Forman

3/13/2008 1:00:00 AM

0

On Mar 12, 5:42 pm, "Andrew Rekdal" <as...@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

Steven D'Aprano

3/13/2008 1:34:00 AM

0

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



hdante

3/13/2008 1:38:00 AM

0

On Mar 12, 9:42 pm, "Andrew Rekdal" <as...@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

class BigObject:
import my_module
def __init__(self):
self.my_module.do_this()

---------------------------------------

class BigObject(MyUtils):
def __init__(self):
self.things_from_utils()

Andrew Rekdal

3/13/2008 1:51:00 AM

0

Well, I can see how this could get real messy but within defining a GUI
there are many elements and so the block of elements such as a wx.notebook
for instance I would hope I could place all the code for this in another
file and somehow include it into place. This way I can work on layered
panels and such in a fresh document rather than travesing through tons of
widgets and sizers.

Thanks for your replies

--
-- Andrew

"Steven D'Aprano" <steve@REMOVE-THIS-cybersource.com.au> wrote in message
news:13th14pe0hhsie1@corp.supernews.com...
> 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
>
>
>


hdante

3/15/2008 11:49:00 PM

0

On Mar 12, 10:50 pm, "Andrew Rekdal" <as...@comcast.net> wrote:
> Well, I can see how this could get real messy but within defining a GUI
> there are many elements and so the block of elements such as a wx.notebook
> for instance I would hope I could place all the code for this in another
> file and somehow include it into place. This way I can work on layered
> panels and such in a fresh document rather than travesing through tons of
> widgets and sizers.
>
> Thanks for your replies
>
> --
> -- Andrew
>
> "Steven D'Aprano" <st...@REMOVE-THIS-cybersource.com.au> wrote in message
>
> news:13th14pe0hhsie1@corp.supernews.com...
>
> > 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


You should define your objects in a recursive way. The main window
object only deals with their immediate children. Each childen should
have a class that deals with their immediate children. This way, each
constructor should have a dozen or so children and each of them should
easily fit in a single file.

Or you should be using a GUI editor.