# Class that converts strings to uppercase class UppercaseWord: def __init__(self, word): self.word = word def make_upper(self): return self.word.upper()
# Let's instantiate the class cap_word1 = UppercaseWord(word='hi there') # Let's call the instance method print(cap_word1.make_upper())
HI THERE
Think of a Python class as a blueprint that creates multiple object instances which all have the same behaviors. A class is recommended to have only one responsibility. The above simple class is responsible for turning text into uppercase.
A class generally needs to have an __init__ method. When an instance of a class is created, Python executes the __init__ method of the class. Therefore, inside __init__ you may want to put the code that you want to be executed when an instance is created. In other languages, __init__ is referred to as the constructor.
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.