[Tutor] help with 'organization'

James Reynolds eire1130 at gmail.com
Thu Aug 25 21:12:00 CEST 2011


On Thu, Aug 25, 2011 at 1:51 PM, John <washakie at gmail.com> wrote:

> Hello, I am writing a module that will have two classes ('runners') I
> am calling them, as they will ultimately use subprocess to run a
> command line program, so they are basically option parsers, etc...
>
> As I wrote the second 'runner', I realized many of the methods are
> going to be the same as the first, so I would actually like to create
> a third class that has the methods which are mutual between the two.
> The problem are the 'self' calls....
>
> 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...
>
> class MyTools:
>    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()
>
> if __name__ == "__main__":
>    mp = MyProcess()
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



I am having a very difficult time following what you are trying to do, but
then, i'm not that bright, so it's probably me.

But, I will say this:

class MyTools:
   def f(self, this):
       print(this)

   def foo(self):
       this = self.this
       print(this)

In the second method (foo) you have it creating a local variable called
this, which is equal to the class variable self.this. Why not just use the
class variable throughout that method?

So, print(self.this).

My second thing, and this is just a personal pet peeve, why not create
descriptive names for your variables? Creating things called this or that
(and in this case, literally this or that) is hyper confusing.

My third thing. I think you are going to end up with an attribute error
because I don't see where the class has ever created a class variable called
this.

Maybe you meant to do something like this instead:

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

 def f(self):
       print(self.this)

   def foo(self):
       print(self.this)

so in your other object, it would then look something like this:

class MyProcess:
   def __init__(self):

       self.this = 'My Process, this'

       self.tools = MyTools(self.this)



       self.tools.f()

       self.tools.foo()



But, every time you instantiate MyProcess (not call it) you are going to
call the two methods of Tools class. If you want to seperate this, so you
can do something like tools.foo() and then later on go tools.f() you can do
that in a few ways:

One, you can just get rid of


       self.tools.f()

       self.tools.foo()

and once you create a MyProcess object you are free to call tools methods at
your leisure. For example
m = MyProcess()
m.tools.foo()
m.tools.f()

Unless you really do want those methods once you create the instance because
it's relevent to the creation of the object, then leave them where they are.

Lastly, I don't understand what you mean by "self" calls. This seems rather
academic to me, do you have a real example? I also don't understand what a
Runner is. Maybe its some compsci term or something.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110825/42615220/attachment-0001.html>


More information about the Tutor mailing list