One of the basic building blocks of Python is classes. We’ve already covered the basics of functions and classes, this post will be more of a quickstart with classes in Python. For the video version:
Creating a Simple Class in Python
Alright let’s get started – the only thing you need to know about classes to get started is that they’re just custom objects. They have properties, a constructor/initializer, and methods. Let’s take a look at a simple
class Shape: # declare a class property dimensions = 2
A class is not required to have properties and methods, simply declaring a property makes this a valid class. However, this is pretty useless, so let’s add a constructor/initializer method to our class. Constructors in Python are declared with the keyword
__init__ (now you know why I call them initializers too). For our
Shape constructor we’ll pass in one parameter – a number of sides. All we’ll do here is set
self.sides – the
sides property of the Shape – to the passed in
class Shape: # declare a class property dimensions = 2 # declare a constructor/initializer def __init__(self, sides): self.sides = sides
Now when we create a
Shape we’ll need to declare the number of sides it has. Our
Shape object is still missing something, let’s give it another method. We’ll have our new method validate our
Shape. All we need to do here is check that our
Shape has more than two sides.
class Shape: # declare a class property dimensions = 2 # declare a constructor/initializer def __init__(self, sides): self.sides = sides self.validator() # declare a class function def validator(self): assert self.sides > 2
Now when we try to create a
Shape object with two sides we should get an Assertion Error like the image below.
Extending a Class in Python (Subclasses)
Okay now check this out, this next part is cool. You can “extend” classes into subclasses and they will “inherit” properties from the super class. In our example of
Triangle is a subclass of
Triangles have access to all the same properties and methods as
Shape but also have additional properties and/or methods.
We’ll make a
Triangle class that extends a
Shape with a constructor that takes three parameters for the side lengths. In our constructor, we’ll call
super() to call the constructor for
Shape with three sides and set our
self variables equal to the passed in parameters. We’ll also have a validate function in
Triangle. This time our function is called
__validate, pay attention because this is important – the double underscore in front of the function name indicates this is a private function. This means it can only be accessed from inside the class. Our
Triangle validate function will check that the passed in sides form a valid triangle.
class Triangle(Shape): # triangle constructor def __init__(self, a, b, c): super().__init__(3) self.a = a self.b = b self.c = c self.__validate() # private function to class def __validate(self): assert self.a + self.b > self.c assert self.a + self.c > self.b assert self.b + self.c > self.a
If we try to call
Triangle to create a degenerate
Triangle like one with sides of length 1, 2, and 3, we should get an assertion error like the image below.
We should be able to create a
Triangle with sides of length 2, 2, and 3 though.
Now let’s also try to call
__validate just to validate (pun intended) that it’s a private function. We should see an Attribute Error like the image below.
I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.DonateDonate monthlyDonate yearly