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

Martin MOKREJŠ mmokrejs at ribosome.natur.cuni.cz
Tue Mar 8 14:25:54 CET 2005


Kent Johnson wrote:
> 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)

Oh, I've picked up not the best example. I wanted to set the variables
not under __init__, but under some other method. So this is actually
what I really wanted.

class klass(somefile2.base):
   def __init__():
       pass

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

Thanks!
Martin



More information about the Python-list mailing list