#An example of a class

class Shape:

def __init__(self,x,y):

self.x = x

self.y = y

description = "This shape has not been described yet"

author = "Nobody has claimed to make this shape yet"

def area(self):

return self.x * self.y

def perimeter(self):

return 2 * self.x + 2 * self.y

def describe(self,text):

self.description = text

def authorName(self,text):

self.author = text

def scaleSize(self,scale):

self.x = self.x * scale

self.y = self.y * scale

What you have created is a description of a shape (That is, the variables) and what operations you can do with the shape (That is, the fuctions). This is very important - you have not made an actual shape, simply the description of what a shape is. The shape has a width (x), a height (y), and an area and perimeter (area(self) and perimeter(self)). No code is run when you define a class - you are simply making functions and variables.

The function called __init__ is run when we create an instance of Shape - that is, when we create an actual shape, as opposed to the 'blueprint' we have here, __init__ is run. You will understand how this works later.

self is how we refer to things in the class from within itself. self is the first parameter in any function defined inside a class. Any function or variable created on the first level of indentation (that is, lines of code that start one TAB to the right of where we put class Shape is automatically put into self. To access these functions and variables elsewhere inside the class, their name must be preceeded with self and a full-stop (e.g. self.variable_name).

Using a class

Its all well and good that we can make a class, but how do we use one? Here is an example, of what we call creating an instance of a class. Assume that the code example 2 has already been run:

Code Example 3 - Creating a class

rectangle = Shape(100,45)

What has been done? It takes a little explaining...

The __init__ function really comes into play at this time. We create an instance of a class by first giving its name (in this case, Shape) and then, in brackets, the values to pass to the __init__ function. The init function runs (using the parameters you gave it in brackets) and then spits out an instance of that class, which in this case is assigned to the name rectangle.

Think of our class instance, rectangle, as a self-contained collection of variables and functions. In the same way that we used self to access functions and variables of the class instance from within itself, we use the name that we assigned to it now (rectangle) to access functions and variables of the class instance from outside of itself. Following on from the code we ran above, we would do this:

Code Example 4 - accessing attributes from outside an instance

#finding the area of your rectangle:

print rectangle.area()

#finding the perimeter of your rectangle:

print rectangle.perimeter()

You've reached the end of published parts.

⏰ Last updated: Oct 04, 2008 ⏰

Add this story to your Library to get notified about new parts!

python tutorialsStories to obsess over. Discover now