[Tutor] help with 'organization'

Steven D'Aprano steve at pearwood.info
Fri Aug 26 03:48:34 CEST 2011


John wrote:

> I know a code example might help, so I try to show it here (my code
> I'm afraid is too complex and ugly at the moment). You can see the it
> fails because MyTools doesn't have 'this' attribute...

Then give it one.

> class MyTools:

Add an initialisation method:

     def __init__(self, this):
         self.this = this

>     def f(self, this):
>         print(this)
> 
>     def foo(self):
>         this = self.this
>         print(this)
> 
> class MyProcess:
>     def __init__(self):
>         self.tools = MyTools()
>         self.this = 'My Process, this'
>         self.tools.f(self.this)
>         self.tools.foo()

And change this to:

     def __init__(self):
         self.this = 'My Process, this'
         self.tools = MyTools(self.this)
         self.tools.f(self.this)
         self.tools.foo()



There are probably better, or at least other, ways to organise the code 
which will do the job. An abstract base class with just the shared 
methods might work. (I'm not sure if that's best described as a mixin or 
not...)

class SharedMethods(object):
     # shared methods go here

class FirstRunner(SharedMethods):
     # methods unique to FirstRunner

class SecondRunner(SharedMethods):
     # methods unique to SecondRunner




-- 
Steven


More information about the Tutor mailing list