Greetings all,
        I hope someone can help me.

I have two modules, both define the same set of classes (with differing
implementations) and a number of utility functions that use those classes.

The utility functions are identical (the differences I need are abstracted
in the classes), so they are cut'n'pasted into both files.

A simplified concrete example (not what I'm doing, but it illustrates the
point):

-- C.py --
|class Comment:
|    def __init__(self, value):
|        self.value = value
|
|    def __str__(self):
|        return "/* " + self.value + " */"
|
|# utility functions
|import time
|def stamp():
|    return Comment(time.asctime())


-- P.py --
|class Comment:
|    def __init__(self, value):
|        self.value = value
|
|    def __str__(self):
|        return "# " + self.value + "\n"
|
|# utility functions
|import time
|def stamp():
|    return Comment(time.asctime())


How can I refactor these modules to avoid the code duplication?

I tried:

|class Comment
|    ...
|from utils import *

where utils.py contains the functions, but when I try to use C.stamp(), I
get:

>>> import C as lang
>>> lang.stamp()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "utils.py", line 3, in stamp
    return Comment(time.asctime())
NameError: global name 'Comment' is not defined

Any suggestions how I could (for instance) emulate a #include "utils.py" or
any other technique to eliminate the dreaded cut'n'paste?

Thanks.
        Nigel

-- 
        Nigel Rowe
        A pox upon the spammers that make me write my address like..
                rho (snail) swiftdsl (stop) com (stop) au

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to