Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • ValueError: invalid literal for int() with base 10 Value Error
  • What is Julia used for? Julia programming
  • Recursion Definition
  • YouTube channel lists – Python working with files Python working with files
  • How To Create An Empty Dictionary In Python python dictionaries
  • How To Add Values to a Python Dictionary Python
  • TypeError: not all arguments converted during string formatting Python
  • IndexError: single positional indexer is out-of-bounds Index Error

python classes

Posted on January 12, 2021April 29, 2021 By admin

Estimated reading time: 5 minutes

Recently on our Data Analytics Ireland YouTube channel, we have been working hard to enhance our video content and delivery. As part of that process, we also looked at ways to understand classes and use them more efficiently.

In many programming languages and web development, you will find the use of classes, what you learn here may help you in those as well.

In how to create a class in python we provided a video tutorial of the steps involved in Python on how to implement a class within your project.

Here in this blog posting, we will go through the different aspects of classes, and provide a practical example of Object-Oriented Programming.

Also when you create objects it can help you to manage and reduce the code you may have to write.

Before we start, the first question we should ask is, what is a class?

According to the Official Python Website  “Classes provide a means of bundling data and functionality together”.

So in essence, what they are really saying is that they define a class as centralising information and functionality around a python object.

Python Objects

So how would we describe an object? An object is anything that can have attributes attached to it and have some functionality that allows the object to function.

Most objects will have methods associated with them, and these are the functionality of the object. So let’s step back a second and show this in a piece of code:

“Car ” above is the class object, and it has attributes of type, color, wheels, and doors. Other attributes can be added at any point. So you may add why is start structuring like this important?

The pure and simple answer is organisation!

The reason behind this way is that as the car and its details are all in one place it encourages:

  • Consistency – everything about a car is documented in one place.
  • No duplication – If you were referencing the object car in a number of places in your code, for each update, you would have to change it in each place, it makes the updates long and harder as you have to remember where you put it in the different parts of your computer program.
  • Can be called from anywhere – As we have one version of the object car, now anywhere in our code we can call it and use its attributes, as there is only one version makes the program a lot easier to manage.

Methods and Functions

Now that we have looked at attributes, what about the class method and functions that can be contained within them?

The methods and functions will operate like any other method or function, but the difference when they come to objects is:

They are specific to that object!

python classes

In the above code, you will see that there are three methods, and all are specific to the object car.

For example, you would not expect to see any methods that would relate to

  • pumping up the wheels.

         or

  • changing a bulb.

purely because this object car is only concerned with the functionality of the car.

To build on my point above around no duplication, if this object was not created, this last piece of code:

  • might have to be maintained and duplicated a number of times within your code, this is where the classes come into their own.

So say I want to use the class and its attributes, how would I go about doing that?

In section  9.3.5. Class and Instance Variables on Python classes, it states the following:

Instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:

Anywhere in your code, all you need to do is create a variable and equal it to the class, see below.

Lines 22,23,24, call the method they refer to that is saved in the class, and their respective functionality.

As can be seen, lines 25,26,27,28 all bring in the attributes of the class to be used, by putting them in under the “def__init__” method.

And here is the output of the above. After initiating the class, we have assigned its attributes into a new variable.

And there you go we have initialised the class Car and used its attributes and methods outside the class in our regular programming,

Consequently, this could be done anywhere in our program, multiple times, but only having to use one class.

You can see this working below:

class Tags:class attributes, class initialisation, class methods, Object Oriented Programming, Python, python class, python objects

Post navigation

Previous Post: python sort method
Next Post: How do I fix TypeError: unhashable type: ‘list’ Error?

Related Posts

  • TypeError: ‘NoneType’ object is not iterable class
  • create read update delete using Tkinter class
  • How to Automate Testing With Python | unittest automation
  • raise an exception in python class
  • how to create an instance of a class class
  • How to create a class in Python class

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • What is a Primary Key and Foreign Key SQL
  • how to build a machine learning model machine learning
  • What is data profiling and its benefits? data profiling
  • What is a Unique key in SQL? SQL
  • TypeError: ‘list’ object is not an iterator Python
  • ValueError: cannot convert float NaN to integer Null values
  • What is GITHUB, and should I use it? github
  • What is Query Optimization in SQL? SQL

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT