instances

Q

Quenton Bonds

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.
 
P

Paddy

Quenton said:
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.
Hi Quentin,
Might you be new to programming, or new to Object Oriented programming?
You might profit from reading some of the beginners tutorials mentioned
here:
http://wiki.python.org/moin/BeginnersGuide
which points to
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Here is some info to correct your statements above (but it is not a
tutorial)!

The class statement creates a 'class definition'.
E.g:
class my_class():
pass
Instances of a class are created by using the class name followed by
parenthesis,
E.g:
my_inst = my_class()

If a class has methods:
class my_class2:
def my_method(self):
pass
Then an instance of the class:
my_inst2 = my_class2()
Can access its method like this:
my_inst2.my_method()

Trying to access a method of the class that does not exist will produce
an error.
E.g:
my_inst2.missing_method()
Will give an error, (throw an exception in Python terminology)..

The above is just a (poor) taste. Dive into one of the beginners
tutorials.

- Pad.
 
S

Simon Forman

Quenton said:
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
 
N

Nick Vatamaniuc

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:.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top