Another OOP question.

Peter Hansen peter at engcorp.com
Sun May 26 01:13:41 EDT 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?

Almost, but not quite, at least in your terminology.

Replace all instances of "class" with "object" or "instance" after you say 
"Let me see..." and you'll have it about right...

(The class is like a template for objects.  You instantiate or create
objects "outside" the class, and those are what "self" refers to in the
template: a future object-to-be, fresh from the mould that is the class.)

You might also use "field" instead of "variable" in the above, to be
even closer to classic usage, or maybe "attribute" or "property".
These data items held in an object are usually distinguished in this
way from those variables which might exist only temporarily inside one 
of the methods (what you call a function inside a class).

-Peter



More information about the Python-list mailing list