471,596 Members | 1,002 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

Partial Classes - Aspects

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?

Thanks,
Peer

Jul 29 '06 #1
0 1288

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Gomaw Beoyr | last post: by
16 posts views Thread by pawel.pabich | last post: by
1 post views Thread by Bishoy George | last post: by
9 posts views Thread by Fat Elvis | last post: by
5 posts views Thread by Sagar | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.