tags:

views:

313

answers:

5

I have read several documentation already but the definition of "class" and "instance" didnt get really clear for me yet.

Looks like that "class" is like a combination of functions or methods that return some result is that correct? And how about the instance? I read that you work with the class you creat trough the instance but wouldnt be easier to just work direct with the class?

Sometimes geting the concepts of the language is harder than working with it.

+7  A: 

Your question is really rather broad as classes and instances/objects are vital parts of object-oriented programming, so this is not really Python specific. I recommend you buy some books on this as, while initially basic, it can get pretty in-depth. In essense, however:

The most popular and developed model of OOP is a class-based model, as opposed to an object-based model. In this model, objects are entities that combine state (i.e., data), behavior (i.e., procedures, or methods) and identity (unique existence among all other objects). The structure and behavior of an object are defined by a class, which is a definition, or blueprint, of all objects of a specific type. An object must be explicitly created based on a class and an object thus created is considered to be an instance of that class. An object is similar to a structure, with the addition of method pointers, member access control, and an implicit data member which locates instances of the class (i.e. actual objects of that class) in the class hierarchy (essential for runtime inheritance features).

So you would, for example, define a Dog class, and create instances of particular dogs:

>>> class Dog():
...     def __init__(self, name, breed):
...             self.name = name
...             self.breed = breed
...     def talk(self):
...             print "Hi, my name is " + self.name + ", I am a " + self.breed
...
>>> skip = Dog('Skip','Bulldog')
>>> spot = Dog('Spot','Dalmatian')
>>> spot.talk()
Hi, my name is Spot, I am a Dalmatian
>>> skip.talk()
Hi, my name is Skip, I am a Bulldog

While this example is silly, you can then start seeing how you might define a Client class that sets a blueprint for what a Client is, has methods to perform actions on a particular client, then manipulate a particular instance of a client by creating an object and calling these methods in that context.

Sometimes, however, you have methods of a class that don't really make sense being accessed through an instance of the class, but more from the class itself. These are known as static methods.

Paolo Bergantino
So basicly you define a "class" and then associate objetcs with it (wich combine state, behavior and methods) ?For example if i want to manipulate images that I upload to a site, first I create a class Images inside a module, like image.py, then inside it I create all objects that will manipulate the images?
fabio
inside it you create all _methods_ that manipulate the image. The objects would be the specific images you want to do something with. So after you define your class and your methods, you might do: foo = Image('/home/me/whatever.jpg') foo.resize(500) - you would then be acting on the particular instance of the image you instantiated.
Paolo Bergantino
Now im starting to understanding it. So instead of creating several functions around (in some loose way) I can create, instead, a class and then several methods associate with it so i can do everything i want to it inside that class (on its methods)?
fabio
Correct. A class is a variable type. you declare and instantiate the variable to hold the values you want, then any function you perform on this will be performed on these values. there is only one class, but you can have as many instantiations as you want.
Narcolapser
A: 

In part it is confusing due to the dynamically typed nature of Python, which allows you to operate on a class and an instance in essentially the same way. In other languages, the difference is more concrete in that a class provides a template by which to create an object (instance) and cannot be as directly manipulated as in Python. The benefit of operating on the instance rather than the class is that the class can provide a prototype upon which instances are created.

cmsjr
+2  A: 

It's fairly simple actually. You know how in python they say "everything is an object". Well in simplistic terms you can think of any object as being an 'instance' and the instructions to create an object as the class. Or in biological terms DNA is the class and you are an instance of DNA.

class HumanDNA(): # class
   ... class attributes ...

you = HumanDNA() # instance
SpliFF
I really liked the comparasion you did with the dna and the person, got the point.
fabio
+3  A: 

I am not sure of what level of knowledge you have, so I apologize if this answer is too simplified (then just ignore it).

A class is a template for an object. Like a blueprint for a car. The instance of a class is like an actual car. So you have one blueprint, but you can have several different instances of cars. The blueprint and the car are different things.

So you make a class that describes what an instance of that class can do and what properties it should have. Then you "build" the instance and get an object that you can work with.

FeatureCreep
+1  A: 

See http://homepage.mac.com/s_lott/books/python/htmlchunks/ch21.html

Object-oriented programming permits us to organize our programs around the interactions of objects. A class provides the definition of the structure and behavior of the objects; each object is an instance of a class.

Objects ("instances") are things which interact, do work, persist in the file system, etc.

Classes are the definitions for the object's behavior.

Also, a class creates new objects that are members of that class (share common structure and behavior)

S.Lott
Fight bitrot! http://homepage.mac.com/s_lott/books/python/html/p03/p03c01_class.html
Tobu