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

Scott David Daniels Scott.Daniels at Acm.Org
Tue Mar 8 09:16:53 EST 2005


Martin MOKREJŠ wrote:
> ....  If I put them into a module, it get's executed only once unless I 
 > do reload. And I'd have to use: "from some import *",
> because mainly I'm interrested in assigning to self:
> self.x = "blah"
> self.y = "uhm"

OK, somewhere in here I think I get what you want to do.  Essentially
you want to set a lot of attributes on some object which is almost
always named "self", and the "set a lot of attributes" varies as
separate chunks.  The perfect application for a function (not a _pure_
function in the functional programming sense).  So, it is my opinion
that you want to define a function to call, not include code from some
other file.

How about:

Here are some "whole lot of variables" functions, put them in 'code.py':

     def do_a_bunch(obj):
         obj.a = 123
         obj.b = 3.141529
         obj.c = 'what on earth?'
         obj.author = u'Charles Dickens'
         ...

     def do_other_stuff(obj):
         obj.a = 123456789
         obj.b2 = 3.141529 ** .5
         obj.c = u'Where in Jupiter?'
         obj.author = u'Martin MOKREJŠ'
         ...

And here is how you use them:

     from code import do_a_bunch, do_other_stuff

     class SomethingOrOther(SomeSuperClass):
         def __init__(self, stuff, nonsense):
             SomeSuperClass.__init__(self, stuff)
             self.fiddle(nonsense)
             do_a_bunch(self)

         def some_other_method(self):
             ...
             do_a_bunch(self)


         def mangle(self):
             ...
             do_other_stuff(self)


> I'm newbie, sure.
That is why I was trying to figure out your original requirement,
not how to accomplish your original plan.  I was trying to see if
there was a good reason you needed to use "#include" - like behavior.
Does something like this address your problem?

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list