By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Partial Classes - Aspects

P: n/a
I'm currently writing a small toy application to support symbolic
algebra. Therefore I implemented some classes Term, Var, Number, Sum,
Product, Power. These classes are tightly coupled. So it is not
possible to organize them in distinct files. This would result in
cyclic imports.

To manage the complexity I implemented some sort of aspect oriented
programming (perhaps aspect oriented programming is not quite right in
this context...). That is I implemented a mechanism to dynamically add
methods to existing classes. This is similar to the thread "Partial
classes" discussed in this list. Therefore hereby offer some use case
for partial classes.

The aspects I already implemented are amongst others: pretty printing,
differentiation, expansion of terms. The aspect for expansion looks
like this:
# -*- coding: iso-8859-1 -*-
__aspect__ = "Expand"

class TermExpand:
def expand(self):
return self

class SumExpand:
def expand(self):

def expandSum(term, sum):
# some helper function

class ProductExpand:
def expand(self):

class PowerExpand:
def expand(self):

The code to import an aspect into existing classes accepts as a
parameter the name of a module. The module will be imported.
Afterwards the code iterates over all elements defined in the module.
If it is a (specially named) class all methods will be copied to the
original class. If it is a (top level) function it will be copied into
the global namespace. E.g. the method expand of the class TermExpand
will be copyied to the base class Term.

The difficulty with this approach is that the classes' methods and the
functions do operate in the wrong global namespace. Since these
functions/methods are located in their own module they get their own
namespace. To correct this I have to use new.function() to create new
methods/functions with the correct namespace (which is the base
module's namespace).

So my questions boil down to this:

* Do you think this is a sound approach to structure my code?
* I don't like the necessity to modify the functions. Is there a
simpler approach?


Jul 29 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.