instances

Simon Forman rogue_pedro at yahoo.com
Fri Jul 14 15:02:00 EDT 2006


Quenton Bonds wrote:
> Hello
> I am trying to understand the abilities and limitation of creating an
> instance.  First I will give you my understanding then please steer me
> in the right direction.
>

Wow, you've got it nearly completely comprehensively backwards.

> Abiities
> 1.  The two ways to create an instance is def method(self) &
> __int__(self, other, instances,...)

There's really just basically one way to create an instance, and that's
by writing a class and then "calling" it.  (example below)

if you use the def statement by itself, then you are creating a
FUNCTION object, that you later call with arguments to do some work.

When you create a class, it looks like this:

class foo:
    def __init__(self, arg1, arg2):
        # do some work to set up the instance of the class.

and then you "call" the class like so:

bar = foo(arg1, arg2)

to create an INSTANCE of the CLASS.  the name 'bar' now references an
instance of class 'foo'.

(also note that when the def statement is used within a class, like the
__init__ above, then it creates a METHOD, which is almost the same
thing as a FUNCTION.)

> 2.  By creating an instance of a method; the functions of that method
> can be used through out the
>    program in a fashion such as self.methodofprogram(parameters)

Ok, I think the easiest thing to do here would be to rewrite your
sentence using the proper terminology:

By creating an instance of a CLASS, the METHODS of that CLASS can be
used through out the program in a fashion such as
INSTANCE.methodofCLASS(parameters)

Example of creating a class with a method:

class foo:
    def __init__(self, arg1):
        # do some work to set up the instance of the class.
        self.value = arg1
    def printme(self):
        print self.value

Example of creating an instance of that class:

bar = foo('Hi there!')


Example of using a method of an instance of that class:

bar.printme()

# Prints "Hi there!"



> Limitations
> 3.  One cannot create an instance of a class.

:)  One can ONLY create instances of classes.


> 4.  An instance can only perform functions that are provided from the
> method it was instanced from.

Yes, *IF* you replace "method" in that sentence with "class",  and
"functions" with "methods".


> 5.  Is there any other key information I am missing.

I hope this helps,
~Simon




More information about the Python-list mailing list