Another OOP question.

Sean 'Shaleh' Perry shalehperry at attbi.com
Sun May 26 00:58:05 EDT 2002


On 26-May-2002 SA wrote:
> The following is an example class from a tutorial:
> 
> class Message:
>     def __init__(self, aString):
>         self.text = aString
>     def printIt(self):
>         print self.text
> 
> Let me see if I am understanding the structure correctly. __init__ is used
> first to initialize the class. Self refers to the class. Astring is the
> variable passed to the class from code outside the class. PrintIt is called
> from outside code to print the variable loaded in the class.
> 
> Ok. Given that this is an overly simplistic explanation, is this the correct
> interpretation of this class?
> 
> Thanks. I hope I'm finally getting OOP.
> 

that is about it.  We need to clean up some terms though.

foo = Message("this is a test")

'foo' is an instance of the class Message.  In the __init__ method 'self'
refers to the *instance* of a class not the class itself.  It is the OOP
equivalent of "my __ is __", i.e. self.name "my name is __".

printIt is usually referred to as a 'method' of class Message.  So someone
refers to it as 'and then you call the printIt method of class Message to
retrieve the text'.  You may also see some texts refer to calling a method as
'sending a message to the class'.

and to ties this into python, the Message class looks a lot like the Exception
class.





More information about the Python-list mailing list