On 01/21/2014 05:20 AM, Christian Alexander wrote:
Alan,

The concept and purpose of classes is starting to sink in a little bit, but
I still haven't had my "Ah-ha" moment yet.  I just can't seem to visualize
the execution of classes, nor am I able to explain to myself how it
actually works.   For example:


     class Person:
         def __init__ (self, name, age):        # is self just a placeholder
for  an arbitrary object?  How does __init__ actually work?
             self.name = name                         # why assign name to
self.name variable?
             self.age  = age                               # same as previous
         def salute (self):                                 # again with the
self
             print ("Hello, my name is " + self.name +
                 " and I am " + str(self.age) + " years old.")

[PLease avoid top-posting, and quote only the parts of the post you actually reply to.]

Yes, self is just a placeholder, as I said in the post you reply to. (I used the same word.) It is a placeholder for *whatever object it operates on, now*. If you call a Person methon on 'x', then x is assigned to self.

We assign name & age tp self, meaning to x, because a person is defined as a composite piece of data {name, age}. Read again the very start of my previous post, where I wrote the definition of p1, directly, in an imaginary language:

   p1 = {name="Maria", age=33}     # no good python code

This is a programming of a person, as we need. This means, for this application, we need a person to be defined by these 2 relevant properties, both of them, and nothing more.

__init__ works as if we had programmed that way:

===============
class Person:
    def init (self, name, age):
        self.name = name
        self.age  = age
    def salute (self):
        print ("Hello, my name is " + self.name +
            " and I am " + str(self.age) + " years old.")

p1 = Person()
p1.init("Maria", 33)
p1.salute()
================

except that we don't need to call it. Do you understand the principle of (1) defining a function with potential input variables (2) executing it on actual input variables? In python the "potential input variables" are called "parameters", while "actual input variables" are called "arguments".

There is a similar relation between a class and objects of that class. A class defined potential objects, with potential attributes (here name & age); "instances" are actual objects of that class with actual attributes.

For the matter, I don't think your difficulties with these notions are related to you beeing a visual thinkers: I am, too, and strongly so (and after all, the notion of type is firstly visual: think at naive species).

Denis
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to