instances

Nick Vatamaniuc vatamane at gmail.com
Fri Jul 14 15:08:38 EDT 2006


Quenton,
What kind of instances do you want to create? An instance has to be an
instance of something. You  mention creating instances "of a method",
what do you mean by that?

Anyway, assuming you are new to Python here is a basic intro about
objects and classes:
Think of a class as a blueprint and the objects are object built from
that blueprint. The object are called 'instances' of the class.
Creating an object of a specific class is called instantiating. So if
you have a blueprint for a car you use it to make a car. A car is an
instance instantiated from the car's blueprint.
Simple Example:
class Car:
    def __init__(self,color):
       self.color=color
    def drive(self):
       print "The",self.color,"car is driving"

That's  a simple Car class i.e. the blueprint. It will make any car of
a specific color. To make an actual car object do:
jetta=Car("blue")
You have created an _instance_ of a class Car called jetta.  Now,
according to the blueprint, you car will have one method you can call
and you call it like this::
jetta.drive()
and you should get the output "The blue car is driving".

Three things to notice:
1) 'self' is the first argument inside any method of the class. Both
__init__ and drive get it. Self references the object of the class
itself when those methods are called. For example the __init__ method
sets the color of the car. But it has to know where the car object is
so it can set its color. The car object itself is represented by 'self'
and then we set its color using self.color
2) the __init__ method is called a "magic" method. It can be called
_for you_ at a specific time. The __init__ method is used to initialize
a class object. So the object is created by Python then its __init__
method is called after that so you can customize it. Imagine that
Python makes a car for you and then it gives it to your __init__ method
for customization.  Your __init__ method paints it a certain color and
you have your new car ready to go!
3) if you noticed from the semantics or form 2), __init__ does not
create an instance of a class by itself. It only customizes an already
created instance that it gets through the self argument.  There is a
magic method called __new__ that Python uses for you to create an
instance of a class but with very very very rare exceptions you don't
even need to know about it (I don't even know what its parameters are,
because in all these years I have never had to use it).

Hope this helps, I assumed you are a new Python user that is why I
presented a simplistic example. Please see some Python tutorials and
documenation, you can search on Google for them.

Regards,
Nick Vatamaniuc




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.
>
> Abiities
> 1.  The two ways to create an instance is def method(self) &
> __int__(self, other, instances,...)
> 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)
> Limitations
> 3.  One cannot create an instance of a class.
> 4.  An instance can only perform functions that are provided from the
> method it was instanced from.
> 
> 5.  Is there any other key information I am missing.




More information about the Python-list mailing list