Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2005, 06:39 PM
Jerry
Guest
 
Posts: n/a
Default Splitting a class definition into several files?

My "main" class is getting a bit long...Is it possble to split a class definition
into several files and then import the pieces to get the whole definition?

Jerry



  #2  
Old July 18th, 2005, 06:39 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default Re: Splitting a class definition into several files?

Jerry wrote:
[color=blue]
> My "main" class is getting a bit long...Is it possble to split a class
> definition into several files and then import the pieces to get the whole
> definition?[/color]

I think there is some design issue if you really have a problem like this.

Apart from that, it should be possible to compose your main class by
inheriting from several subclasses that are in distinct files, like this:

---file A ---

class A:
def a(self):
pass

---file B ---

class B:
def b(self):
pass

---file main ---

class Main(A,B):
def __init__(self):
pass

--
Regards,

Diez B. Roggisch
  #3  
Old July 18th, 2005, 06:39 PM
Bengt Richter
Guest
 
Posts: n/a
Default Re: Splitting a class definition into several files?

On 15 Nov 2004 15:08:31 -0800, jerry.levan@eku.edu (Jerry) wrote:
[color=blue]
>My "main" class is getting a bit long...Is it possble to split a class definition
>into several files and then import the pieces to get the whole definition?
>
>Jerry[/color]
Ignoring the fact that this is not unlikely a symptom of a design problem ... ;-)

If you import from different files, the code in them will have distinct
globals() dictionaries -- i.e., the module dicts of the respective imported
modules. If that doesn't matter -- as e.g., if you wanted to define the functions
for methods that don't use globals (including the Main class name) then you could
use import to bind methods and initial class variable values -- remembering that
the latterbindingswithin the Main class.
__________________________________________________ ___________________
[color=blue][color=green][color=darkred]
>>> for line in file('main.py'): print line.rstrip()[/color][/color][/color]
...
__________________________________________________ ___________________

class Main(object):
"""
Class Main demonstrates
- importing functions from other module global scope to become methods
showing distinction of global references from imported vs locally defined
method functions.
- including code into class via execfile
- normal method and class variable definitions
"""
# do example imports
from main_methods_1 import main_method_1, trouble, trouble_global, foo_mm1
# from main_methods_2 import ...
# from main_methods_3 import ...

# example execfile
execfile("main_classvar_defs.py")

#normal stuff
def foo(x='foo local'): return locals().keys(), globals().keys()
def normalmethod(self):
"normalmethod defined in class Main of main.py"
return self.normalmethod.__doc__
normal_class_var = 'normal classvar defined in Main of main.py'
__________________________________________________ ___________________
[color=blue][color=green][color=darkred]
>>> for line in file('main_methods_1.py'): print line.rstrip()[/color][/color][/color]
...
__________________________________________________ ___________________

def main_method_1(self):
"main_method_1 defined in main_methods_1.py global scope as function"
return type(self).main_method_1.__doc__ # can't refer to Main.main_method_1

def foo_mm1(x='foo_mm1 local'): return locals().keys(), globals().keys()

trouble_global = 'global defined in global scope of main_methods_1.py'
def trouble(self):
"""
this returns a global, but it's not Main's global
and in fact is also given a Main class var binding via the import *
"""
return trouble_global
__________________________________________________ ___________________
[color=blue][color=green][color=darkred]
>>> for line in file('main_classvar_defs.py'): print line.rstrip()[/color][/color][/color]
...
__________________________________________________ ___________________

cv1 = 'class var cv1 from execfile("main_classvar_defs.py")'
def xfmethod(self):
"defined in main_classvar_defs.py via execfile -- note ref to Main ok"
return Main.xfmethod.__doc__, Main.cv1
def foo_cv1(x='foo_cv1 local'): return locals().keys(), globals().keys()
__________________________________________________ ___________________

Now, importing main:
[color=blue][color=green][color=darkred]
>>> import main
>>> help(main)[/color][/color][/color]

Help on module main:

NAME
main

FILE
c:\pywk\clp\jerry.levan\main.py

CLASSES
__builtin__.object
Main

class Main(__builtin__.object)
| Class Main demonstrates
| - importing functions from other module global scope to become methods
| showing distinction of global references from imported vs locally defined
| method functions.
| - including code into class via execfile
| - normal method and class variable definitions
|
| Methods defined here:
|
| foo(x='foo local')
| #normal stuff
|
| foo_cv1(x='foo_cv1 local')
|
| foo_mm1(x='foo_mm1 local')
|
| main_method_1(self)
| main_method_1 defined in main_methods_1.py global scope as function
|
| normalmethod(self)
| normalmethod defined in class Main of main.py
|
| trouble(self)
| this returns a global, but it's not Main's global
| and in fact is also given a Main class var binding via the import *
|
| xfmethod(self)
| defined in main_classvar_defs.py via execfile -- note ref to Main ok
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __dict__ = <dictproxy object at 0x009AEFF0>
| dictionary for instance variables (if defined)
|
| __weakref__ = <attribute '__weakref__' of 'Main' objects>
| list of weak references to the object (if defined)
|
| cv1 = 'class var cv1 from execfile("main_classvar_defs.py")'
|
| normal_class_var = 'normal classvar defined in Main of main.py'
|
| trouble_global = 'global defined in global scope of main_methods_1.py'
[color=blue][color=green][color=darkred]
>>> mlist = [k for k,v in vars(main.Main).items() if callable(v) and not k.startswith('__')]
>>> mlist[/color][/color][/color]
['xfmethod', 'foo_mm1', 'main_method_1', 'trouble', 'foo_cv1', 'foo', 'normalmethod'][color=blue][color=green][color=darkred]
>>> minst = main.Main()
>>> for m in mlist: print '%14s: %r'%(m, getattr(minst, m)())[/color][/color][/color]
...
xfmethod: ('defined in main_classvar_defs.py via execfile -- note ref to Main ok', 'class
var cv1 from execfile("main_classvar_defs.py")')
foo_mm1: (['x'], ['__builtins__', 'foo_mm1', '__file__', 'trouble_global', 'main_method_1
', '__name__', 'trouble', '__doc__'])
main_method_1: 'main_method_1 defined in main_methods_1.py global scope as function'
trouble: 'global defined in global scope of main_methods_1.py'
foo_cv1: (['x'], ['__builtins__', '__name__', '__file__', 'Main', '__doc__'])
foo: (['x'], ['__builtins__', '__name__', '__file__', 'Main', '__doc__'])
normalmethod: 'normalmethod defined in class Main of main.py'

[color=blue][color=green][color=darkred]
>>> minst.trouble()[/color][/color][/color]
'global defined in global scope of main_methods_1.py'[color=blue][color=green][color=darkred]
>>> minst.trouble_global[/color][/color][/color]
'global defined in global scope of main_methods_1.py'[color=blue][color=green][color=darkred]
>>> minst.trouble_global is minst.trouble()[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> minst.trouble_global is main.Main.trouble_global[/color][/color][/color]
True

<disclaimer>I haven't used these techniques in any significant way, so testing is advised.
They're just what I thought of in response to your post, based on a few previous experiments.
;-)</disclaimer>

Regards,
Bengt Richter
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.