A class contains attributes and functions that indicate the behavior of the attributes. Python being an object-oriented programming language supports classes and objects. Let’s discuss the class, self, attributes, and functions in the upcoming section.
Firstly, What is a class in Python?
We can define a class by using the class keyword. I have created a class called “Tutorial” with a welcome note which is an attribute of the class.
class Tutorial:
note = " Welcome to tutorials about class"
Secondly, Lets learn about class, attributes, functions and objects.
I have created a class called Tutorial and it has a member variable or attribute called note. I add a function called PrintNote which prints the value of the variable note.
You may be wondering what is ‘self‘? Self is a keyword that we use to represent an instance of a class. You may have seen ‘this’ in other languages like C++, Java, etc. ‘Self’ and ‘This’ are the same except that we should explicitly mention self in python.
class Tutorial:
note = " Welcome to tutorials about class"
def PrintNote(self ):
print(self.note)
tuts = Tutorial()
tuts.PrintNote()
#output
Welcome to tutorials about class
When we create an instance of the class the instance here is held in the variable called tuts. An instance of a class is called an object. We can use this object to call the member functions of the class. In addition, all the members in python are public by default.
Thirdly, Lets discuss about the __init__() function.
In other words, we call this a constructor. A constructor is called when I instantiate the object. Since this is the first function that will be invoked at instantiation, we can set the default values for the member variables.
class Tutorial:
note = " Welcome to tutorials about class"
other =""
def __init__(self):
self.other = "Welcome learners, this is the constructor"
print(self.other)
def PrintNote(self):
print(self.note)
tuts = Tutorial()
tuts.PrintNote()
#output
Welcome learners, this is the constructor
Welcome to tutorials about class
In the example above, we can see that when tuts are created the constructor is called and it prints saying “Welcome learners, this is the constructor”. We call the function PrintNote afterward.
Public, Private and protected members
All members are public by default. I can access them from outside the class environment.
We indicate a protected variable by single underscore ‘_’. For example, businessType is a protected variable. Python allows the use of a protected variable outside of a class as a public variable but the user has to be aware of the design aspect of a protected member. The protected members are meant to be used by the sub classes which inherit from ‘Company’ class.
We use double underscore ‘__’ to indicate a private member. In the example, CopyrightStartYear is a private variable. We cannot use the private variable directly outside of the class environment. Python throws an error as shown in the image above.
class Company:
name = "Telestreak"
def __init__(self):
self._businessType = "Website"
self.__CopyrightStartYear = "2020"
comp = Company()
print(comp.name)
print(comp._businessType )
print(comp.__CopyrightStartYear) #error
#output
Telestreak
Website
Want to try for yourself ? Have fun! Feel free to comment the example code samples.
Go back to the tutorial overview
Our subscribers get access to pdf version of many blog posts and much more content.
Next: (coming soon)