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 2148
Jerry wrote: 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?
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
On 15 Nov 2004 15:08:31 -0800, je*********@eku.edu (Jerry) wrote: 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
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.
__________________________________________________ ___________________ for line in file('main.py'): print line.rstrip()
...
__________________________________________________ ___________________
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'
__________________________________________________ ___________________
for line in file('main_methods_1.py'): print line.rstrip()
...
__________________________________________________ ___________________
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
__________________________________________________ ___________________
for line in file('main_classvar_defs.py'): print line.rstrip()
...
__________________________________________________ ___________________
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:
import main help(main)
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'
mlist = [k for k,v in vars(main.Main).items() if callable(v) and not k.startswith('__')] mlist
['xfmethod', 'foo_mm1', 'main_method_1', 'trouble', 'foo_cv1', 'foo', 'normalmethod'] minst = main.Main() for m in mlist: print '%14s: %r'%(m, getattr(minst, m)())
...
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'
minst.trouble()
'global defined in global scope of main_methods_1.py' minst.trouble_global
'global defined in global scope of main_methods_1.py' minst.trouble_global is minst.trouble()
True minst.trouble_global is main.Main.trouble_global
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: M O J O |
last post by:
Hi,
I'm creating an CRM solution for my company.
I want to split up the solution into several classlibraries, so I dont need
to build the entire solution every time I run my project.
First...
|
by: Mon |
last post by:
I am in the process of reorganizing my code and came across and I came
across a problem, as described in the subject line of this posting.
I have many classes that have instances of other classes...
|
by: Bryan Parkoff |
last post by:
CMain Class is the base class that is initialized in main function. CA
Class is the base class that is initialized in CMain::CMain(). CMain Class
is always public while CA Class is always...
|
by: Andy |
last post by:
Hi all,
I have a site with the following architecture:
Common.Web.dll - Contains a CommonPageBase class which inherits
System.Web.UI.Page
myadd.dll - Contains PageBase which inherits...
|
by: Jon |
last post by:
I am not too familiar with working with files, so I'd like some advice. I
need to write a function for my program that take large text files (> 150
MB) and splits them into several text files of...
|
by: jarhead |
last post by:
My Tkinter app's gui file grew to the point that i wanted to split it
into several files: menus.py, mainFrame,py, buttons.py, etc. Of
course, when i moved the menu code into its own file, then did...
|
by: David |
last post by:
Hi,
Using C#, .NET 1.1
I have a class that has many methods. It is my datalayer. Some methods are
related to each other and I want to make my code more manageable by
seperating the related...
|
by: Michael R. Copeland |
last post by:
I have defined the following class:
//-----------------------------------------------------------------
class LogFunctions // Log/Audit File class
{
public:
LogFunctions::LogFunctions(char...
|
by: satyanarayan sahoo |
last post by:
What’s the role of Partial keyword which is bydefault written for a class in 2.0 ?
public partial class Form1 : Form
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |