O'Reilly logo

A Concise Introduction to Programming in Python by Mark J. Johnson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 25

Related Classes

Classes may also be related to each other through a mechanism called inheritance. When one class inherits from another, it inherits all the state and behavior of the original class, and then has the opportunity to define additional new state and behavior.

The Sierpinski triangle is an interesting mathematical object (a fractal, like the Mandelbrot set) that is constructed by repeatedly removing the middle from a solid triangle.

Listing 25.1: Sierpinski Triangle

 1 # sierpinski.py
 2
 3 from turtle import Turtle, setworldcoordinates, exitonclick
 4
 5 class SierpinskiTriangle(Turtle):
 6 size = 2
 7 def __init__(self, n, x, y):
 8  Turtle.__init__(self, visible=False)
 9  self.n = n
10  self.speed(0)
11  self.penup()

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required