looking for way to include many times some .py code fromanotherpython code
Martin MOKREJŠ
mmokrejs at ribosome.natur.cuni.cz
Tue Mar 8 16:36:04 EST 2005
Scott David Daniels wrote:
> 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.
Basically, doing in a class method
def(self, a, b, c):
self.a = a
self.b = b
self.c = c
sounds stupid. With next instance I'll loose a, b, c, so I have to save
then to a variable, "self." prefix is generally proposed way. But
it's not surprising a gets to self.a, right? Actually, I thought about
the *args tuple and even **kwargs, but I thought this will make the core
even less readable. Thinking of it now, you'd probably do
self.__dict__.update(kwargs), right? Hmm, but does it assign to self or not?
I mean, does it equivalent to `a = 1' or `self.a = 1' ? The latter seem to
be true, right?
>
> 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?
This doesn't help me. ;)
More information about the Python-list
mailing list