looking for way to include many times some .py code from anotherpython code

Kent Johnson kent37 at tds.net
Tue Mar 8 08:07:12 EST 2005


Martin MOKREJŠ wrote:
> Hi,
>  I'm looking for some easy way to do something like include in c or PHP.
> Imagine I would like to have:
> 
> 
> cat somefile.py
> a = 222
> b = 111
> c = 9
> 
> 
> cat somefile2.py
> self.xxx = a
> self.zzz = b
> self.c = c
> self.d = d
> 
> 
> cat anotherfile.py
> 
> def a():
>    include somefile
>    postprocess(a)
> 
> def b():
>    include somefile
>    postprocess(a, b, c)
> 
> class klass():
>    def __init__(self, a, b, c, d):
>        include somefile2

You can do this with module-level variables and a base class for klass:

cat somefile.py
a = 222
b = 111
c = 9


cat somefile2.py
class base:
     def __init__(self, a, b, c, d):
         self.xxx = a
         self.zzz = b
         self.c = c
         self.d = d


cat anotherfile.py
import somefile, somefile2

def a():
    postprocess(somefile.a)

def b():
    postprocess(somefile.a, somefile.b, somefile.c)

class klass(somefile2.base):
    def __init__(self, a, b, c, d):
        somefile2.base.__init__(self, a, b, c, d)

Kent

> 
> 
>  I know about module imports and reloads, but am not sure if this is the 
> right
> way to go. Mainly, I want to assign to multiple object instances some 
> self bound
> variables. Their values will be different, so I can't use global variables.
> 
> Martin



More information about the Python-list mailing list