[Pipet Devel] pygtools tutorial - some definitions

J.W. Bizzaro bizzaro at bc.edu
Wed Jun 2 15:11:43 EDT 1999

Danny Rice wrote:
> >Here is a Python example:
> >
> >MyClass:                             # A new class is defined.
> >
> >    def my_method(self, my_number):  # A new method is defined
> >                                     # with the parameter "my_number".
> >        self.my_object = number      # A new object is defined.
> >
> What is "self" here?  A generic parameter or something special?

Self == MyClass or whatever class the code happens to fall under.  So,

    self.my_object = number

is the same as

    MyClass.my_object = number

It's "self-referring".  It is put in as the first parameter passed when the
method is declared so that my_method inherits MyClass's objects.  But it is not,
as I mention below, passed as a parameter by the method call.

> Has "my_object" been declared somewhere else?  Should number be
> my_number?

You're right, it is my_number.  Me bad.

    self.my_object = my_number

_is_ the declaration of the object.  It does not need to be type-defined as with
C.  In C you might type

    int my_object = my_number;

> >my_intance = MyClass()               # An instance is made.
> >my_instance.my_method(2)             # The method is called.
> Why is only 1 parameter passed?

The other parameter, "self", is not passed here because it refers to MyClass,
which is not the class of _this_ block of code but the class of my_method.  So,
confusingly, 1 paramter is passed, but the method lists 2.  It really only
expects one to be passed though (a number or whatever).

> >print my_instance.my_object          # This will print "2".
> It seems like you would need something like "print
> my_instance.my_method.my_object" in case you had multiple methods
> under the class.

Remember that 

    self.my_object = my_number

is the same as

    MyClass.my_object = my_number

Therefore, my_object belongs "under" MyClass in terms of hierarchy, and not
my_method, even though I wrote it in under my_method.  Using "self" makes this
object global to all methods under MyClass.  Finally,

    my_intance = MyClass()



is the "instance of the object".

> This is all pretty confusing to me.

This is really all there is to it.  In the tutorial I sent so far, I explained
most of the basics of PyGTools/Python OOP (PyG POOP?)  If you just get this
stuff down, the rest is pretty simple.

J.W. Bizzaro                  mailto:bizzaro at bc.edu
Boston College Chemistry      http://www.uml.edu/Dept/Chem/Bizzaro/

More information about the Pipet-Devel mailing list