[Tutor] An OO question as relates to Python.

andy surany mongo57a@comcast.net
Mon, 21 Oct 2002 17:29:47 -0400


Thanks Alan - the example is what I was looking for. However, I'm still a
little off track. Maybe what I really need is the ability to establish a
"handle" to a specific frame. What I am doing is this:

Set up a scrolled list, and add data to it (in class ScrolledList). This is
what I would call my "utility" class and it contains a number of utility
methods. Within this class is a generic "refresh the list" routine called
populate_list.

Now I have another class (class WINMENU) which allows a user to operate on
the data in the list. In fact, the list is initially populated by using
ScrolledList(options). After the user has finished operationg on the data, I
need to update the list - but not create a new one. Right now I end up with
2 scrolled lists, 2 sets of buttons, etc. So either I must be executing the
entire ScrolledList class 2x, or I must need some kind of handle to the
original window (or both????). If I have interpreted the example correctly,
this is what I wrote:

    a=ScrolledList(options)
    b=populate_list(a)

I re-read the OOP section of the tutor. I guess that what I want to do is
inherit the attributes of the ScrolledList class to the WINMENU class. Am I
anywhere near the mark? But ScrolledList is not defined at the point WINMENU
is accessed (class WINMENU(Frame,ScrolledList) yields "ScrolledList not
defined"), as follows:

class WINMENU(Frame)
  .....
class ScrolledList(options)
  def populate_list(options)
  .......
If __name__='__main__'
  root=Tk()
  WINMENU(root)
  root.mainloop()

I'm getting dizzy from going around in circles.............

Thanks.

-----Original Message-----
From: alan.gauld@bt.com <alan.gauld@bt.com>
To: mongo57a@comcast.net <mongo57a@comcast.net>; tutor@python.org
<tutor@python.org>
Date: Monday, October 21, 2002 7:21 AM
Subject: RE: [Tutor] An OO question as relates to Python.


>> I understand the concept of classes (or think that I do....)
>
>> In general, how do you maintain a reference to a class
>
>OK, I don't think you really mean this but I'll answer
>it anyway!
>
>A class reference is just like any other variable.
>Thus:
>
>class Foo:
>   def __init__(self): print "Creating a FOO instance"
>
>To create a reference to the class Foo we just assign
>a variable:
>
>Bar = Foo  # note no parens!
>
>Now we can create instances of FOO using either Bar or FOO:
>
>inst1 = Foo()  # one instance of Foo
>inst2 = Bar()  # Also an instance of Foo
>
>That is occasionally useful when dealing with abstract
>frameworks which must create internal instances of
>different classes at runtime(somewhat like C++ templates)
>and readers of my book can find a description in the
>second case study, while others can see the commented
>code on the Usdeless Python website...
>
>However I don't think that really what you want, I think
>you mean how to manage references to different instances,
>and for that you again use variables just as we did
>above with inst1 and inst2.
>
>> - which means, how do I really reference the functionality
>> in a routine belonging to one class from another?
>
>But you really shouldn't do this. (Its possible, but ugly
>and very bad practice!) Instead pass a referenbde to the
>object(instance) and then call the method when required.
>
>Here's an example:
>
>class A:
>  def f(self): print 'in f of A'
>
>class B:
>  def x(self,anObject): self.object = anObject
>  def y(self): print self.object.f()
>
>a = A()  # create an A instance
>b = B()  # create a B instance
>b.x(a)   # pass refernce to a to b
>b.y()    # calls the f method of a.
>
>> Perhaps one of you could refer me to a good online
>> work on the subject...... or throw some good examples my way.
>
>Have you looked at my online tutor topic on OOP?
>
>HTH,
>
>Alan g.
>Author of the 'Learning to Program' web site
>http://www.freenetpages.co.uk/hp/alan.gauld
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor