Python Classes and Objects
What is a class? Why is it being used in Object-oriented programming languages? What is actually an Object-oriented programming?
Do you play games? Yes, a game like Super Mario, Pacman, breakout game, snake game, etc. These different types of games are the part of one big blueprint named as "GAME" :). So there is something which creates a blueprint of various types of games and keeps them together in a player like video games device (eg, Play Station etc).
What is the Class ?
As I explained in above example, Class is just a blueprint which contains stuff like properties, variables, member functions and all. A class definition starts with class keyword followed by class name.
What are Objects:-
Objects can be anything. An object is just an instance of the class which holds the data variables declared in class and the functions work on these class objects.
The syntax for creating Objects in Python
class SampleClass: def function(self): print("Sample function inside a class.") classReference = SampleClass() classReference.function()
self parameter is a reference to the class itself and is used to access variables that belong to the class. classReference is the object of SampleClass() and you can access the functions or variables of the class using . dot operator.
class SampleClass: def function(self): print("Sample function inside a class.") def childFunction(self,sampleParamter): print(sampleParamter) self.function(); classReference = SampleClass() classReference.childFunction("Mani")
In this example, classReference is an object which calls childFunction and the function accepts the parameter and you can use self to call another function in the same class. The code is self-explanatory.
Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.