A class is a programming construct that allows you to group functions and other data. Classes are basically templates for creating objects.
Python is an object-oriented programming (OOP) language that uses classes to define its objects. Basically, everything in Python is an object — lists, tuples, dictionaries, functions, even a string is an object.
All Python objects are based on classes. A class is basically a template for objects. It contains the definition of the object.
You can create your own classes too. The idea behind classes is that they provide a way of grouping functions and other statements that are logically related. For example, you might have a
Customer class for grouping functions and other data related to each customer. You might also have a
Product class for product related data, and so forth.
Create a Class
Here's a quick example to demonstrate the concept:
This creates a class that contains one function. We then create an object from that class (here we call it
o). So once the object has been created, we can work with it.
Create Multiple Objects
We can create many objects based on the same class. In this example we create one object called
o1 and another called
Hello World Hello World
Create Multiple Functions
You'd normally put more than one function in a class. You can also pass arguments to the functions as you would any other function.
Here's an example of a class that groups arithmetic related functions:
5 -292 6 6.0 55 -3245 7623 1.25
Python has a special method called
__init__() that you can add to your class. The
__init__() method is invoked whenever an object is created from the class. Therefore you can make use of the method without having to explictly call it.
Here's a simple example to demonstrate:
Hey Hello World
We can access the
self.a variable using
objectname.variable). So if we had an object called
o1 and another called
o2 then we could use
self bit specifies that the code is for any instance that's created, and that it's not simply a local variable. For example,
self.a means that once we've created an object from this class, we can then access that variable using dot notation (as we did using
o.a). This is known as an attribute reference, commonly shortened to just attribute.
So if we just wanted a local variable to work within the function, we could've used
a = "Hey". But because we wanted to be able to access that variable from our object, we used
self.a = "Hey".
We'll now create a
Person class, for grouping "person" related functions.
First name: Barney Last name: Rubble Email: =================== First name: Barney Last name: Rubble Email: email@example.com
This class uses the
__init__() method for setting various person related attributes.
It also has an
updateEmail() function that can be called when someone's email address needs to be updated.
When we instantiated the class, we set the email address to the empty string (
""), then printed the details (which showed no email address).
We then called the
updateEmail() function, passing in an email address. So when we printed the details again, we saw that the user now has an email address.
Inheritance is a key concept in object oriented programming. Classes can inherit from other classes. This basically means that you can create a class based on another class.
So we could create a
Customer class and base it on the
Person class. We can then add attributes to that only customers will need, such as how much they've spent:
Peter Griffin (customer 12) has spent 13000 in total.
Here we use a (slightly) stripped down version of our
Person class (from the previous example), then we create a
Customer class based on it. We know it's inherited from the
Person class because we've put
Person inside the parentheses (like this
Customer class has a function called
getDetails() that returns a string of text about how much the customer has spent. This function uses the string formatting tokens
%i to insert the customer's details into the right place. The details are provided after the
% symbol after the end of the string.
How to Check an Object's Class
You can check an object's class with the
object.__class__ attribute. Generally, you'll get the same result when using the
Here's how to output the class of an object (using both syntaxes):
<class 'str'> <class 'str'>
What that tells us is that the string is based on the
You can do the same thing for any object. Here are some more examples:
<class 'int'> <class 'float'> <class 'list'> <class 'tuple'> <class 'set'> <class 'dict'> <class 'function'> <class 'builtin_function_or_method'>