looking for way to include many times some .py code fromanotherpython code

Kent Johnson kent37 at tds.net
Tue Mar 8 08:58:28 EST 2005


Martin MOKREJŠ wrote:
> 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)

In that case you can define set_them() directly in the base class and omit set_them from klass:

class base:
   def set_them(self, a, b, c, d):
     self.a = a
     # etc

I'm guessing here, but if a, b, c, d are the same variables defined in your original somefile.py, 
and the intent is to make them attributes of the klass instance, you could do something like this 
and avoid listing all the arguments to klass.set_them():

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

def set_them(obj):
   obj.a = a
   obj.b = b
   obj.c = c

## anotherfile.py

import somefile
class klass:
   def set_them(self):
     somefile.set_them(self)

Kent



More information about the Python-list mailing list