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 2210
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 latterbindingsw ithin 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.p y")
#normal stuff
def foo(x='foo local'): return locals().keys() , globals().keys( )
def normalmethod(se lf):
"normalmeth od defined in class Main of main.py"
return self.normalmeth od.__doc__
normal_class_va r = 'normal classvar defined in Main of main.py'
_______________ _______________ _______________ _______________ _________
for line in file('main_meth ods_1.py'): print line.rstrip()
...
_______________ _______________ _______________ _______________ _________
def main_method_1(s elf):
"main_metho d_1 defined in main_methods_1. py global scope as function"
return type(self).main _method_1.__doc __ # can't refer to Main.main_metho d_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_clas svar_defs.py'): print line.rstrip()
...
_______________ _______________ _______________ _______________ _________
cv1 = 'class var cv1 from execfile("main_ classvar_defs.p y")'
def xfmethod(self):
"defined in main_classvar_d efs.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\jer ry.levan\main.p y
CLASSES
__builtin__.obj ect
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(s elf)
| main_method_1 defined in main_methods_1. py global scope as function
|
| normalmethod(se lf)
| 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_d efs.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.p y")'
|
| normal_class_va r = '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_d efs.py via execfile -- note ref to Main ok', 'class
var cv1 from execfile("main_ classvar_defs.p y")')
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_g lobal
'global defined in global scope of main_methods_1. py' minst.trouble_g lobal is minst.trouble()
True minst.trouble_g lobal is main.Main.troub le_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 I thought about splitting my application up into these libraries:
Solution (solution)
|
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 as member
variables. So including a forward declaration doesnt help, does it?
Faced with these, I had the following options:
-Include the appropriate header in the header file that contains the
class definition that has a member variable that is...
|
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 private. I have placed "friend
void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don't understand why because friend...
|
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 CommonPageBase
- Contains myPage which inherits PageBase
Each of these classes overrides OnInit and ties an event handler
|
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 1000 lines each. What is the
most efficient method of doing this with the framework?
Thanks
--
*********************
| |
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 "import
menus" in the main gui file, it died because of references in the
menus file to stuff in the main gui file. What's the right way, or the
cleanest way, to split up my main gui file into its several conceptual
component files?
tia,
Eric
|
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 methods.
Is there any way I can put my related methods into seperate files while
still maintaining those methods as part of the class?
|
by: Michael R. Copeland |
last post by:
I have defined the following class:
//-----------------------------------------------------------------
class LogFunctions // Log/Audit File class
{
public:
LogFunctions::LogFunctions(char *fileName); // constructor
LogFunctions::~LogFunctions(); // destructor
void LogFunctions::openLog(char *fileName); // open Log file
void LogFunctions::putLog(char *line); // write->Log/Audit file
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |