Python-Ref > Object oriented programming > Basics > Classes and instances
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Classes and instances

A class represents, surprisingly, a class of object. Thus the class "apple" describes an apple - not one conctere apple but the abstract concept of an apple - what properties are found in an apple and what an apple can do.
Once a class is create concrete instances of that class may be created with different values of properties defined by the class. Thus a class is a model (such as mathematical model) and an instance is what you get when you fill the model with concrete values.
Expand/Shrink
Zdroj: (oop2-1.py)
  1   # class definition
  2   class Apple:
  3     """class apple"""
  4   
  5     def __init__( self, color, weight):
  6       self.color = color
  7       self.weight = weight
  8   
  9     def show( self):
 10       print "%s apple weighting %s" % (self.color, self.weight)
 11   
 12   # instance creation
 13   a1 = Apple( "red", "100g")
 14   print "a1 is:",a1
 15   a1.show()
 16   a2 = Apple( "green", "87g")
 17   print "a2 is:", a2
 18   a2.show()
 19   
 20     
stdout:
a1 is: <__main__.Apple instance at 0x2b5aa2e90f38>
red apple weighting 100g
a2 is: <__main__.Apple instance at 0x2b5aa2e9f7e8>
green apple weighting 87g
Doba běhu: 20.7 ms