[Tutor] OOP / Classes questions

Alan Gauld alan.gauld at btinternet.com
Sat Apr 11 01:51:36 CEST 2009


"Stefan Lesicnik" <stefan at lsd.co.za> wrote

> I am a relative newbie to python and OOP concepts and am trying to
> work through wxpython. I've seen understanding classes is essential to
> this and have been trying to work through them.

Steve gave you the basic explanation, I'll try a slightly different tack
- which may or may not help! But its what I find most useful when
working with objects.

> 1. I am not sure why / when you use the self. notation.  I understand
> that self refers to the instance you create, so you can have multiple
> instances and the class knows which one you are talking about.

You just answered your own question :-)

But. I find it best to think about objects as "actors" or robots within
my application that communicate by sending each other messages.

The syntax for sending a message to object foo is

returnValue = foo.message()

So although it *looks* like you are just calling a function, don't think
of it that way, think of it as sending a message. Think that foo might
be a process running on another computer, the notation is just a way
of expressing that you are sending a request to foo, wherever it is,
to perform some operation.

The only way to get an actor to do anything it to tell it to do so via
a message. That means that if an actor needs to do something to
itself it needs to send itself a message. That's when you use self.
When you need to send yourself a  message. Either to access
some data or to initiate a method (methods get invoked as a result
of messages being received - that's why they are different from
functons even if they look the same!)

> Why do they use self.Bind or similar and at other times use file =
> (without the self)

without self its just a local variable or function. With self its a
message to an object, which object just happens to be the same
one sending the message. ( In fact in OOP circles this is actually
referred to as "self messaging" )

> 2. In the Bind( they use self.OnQuit which seems to call the function
> OnQuit). Again, why is it self.OnQuit?

Bind doesn't call OnQuit instead it creates a reference to the
OnQuit method of self. The GUI framework calls that method
when the bound event occurs.

Because OnQuit is a method bind must pass a reference to the
object containing the method as well as the method name. So
the framework then sends a message to the object when the
event ocurs.

> 3. def OnQuit(self, event): - where does event here come from and what
> does it do with it?

The event object is created by the framework when the event occurs.
ie The GUI framework detects that a button has been pressed, a mouse
moved etc... (and The Operating System must detect the event and pass
it to the Framework before that can happen)... then it creates an event
and looks  to see what if any "event handlers" have been registered.
If there is a handler it sends a message to the object/handler concerned
with the event object attached. The handler can then inspect the event
to find out which key was pressed, where the mouse moved to, etc

As I say thats just a slightly different way to think about objects and OOP
I personally find it  very helpful,  you may too. If not, don't worry about 
it! :-)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list