Basic Terminologies of OOP in Python.
Table of Contents
- Class Variables
A class is a blueprint for creating objects, providing initial values for state and implementation of a behavior.
class Dog: pass
Here, we have created an empty dog class. Each dog can have properties like breed, size, age, color, and behaviors like eating, sleeping, sitting, running.
An individual instance of a particular class.
class Dog: pass dolly = Dog()
In the above example, dolly is the object of a class Dog. Creating a new object from a class is known as instantiating an object.
Methods are functions that are defined inside a class and can only be called from an instance of that class.
class Dog: def __init__(self, name, age): self.name = name; self.age = age def getName(self): return self.name
In the above example, there are two methods defined. The
__init__() method is special method. Every time the Dog instance is created the
__init__() method sets the initial state of that object. The getName() method is a method that returns the name of that dog instance.
Class variables are shared by all instances of the class. Class variables are defined inside of class but outside of methods.
class Dog: breed = 'German Shepherd' def __init__(self, name, age): self.name = name; self.age = age
In the above example, the breed is the class variable. Class variables can be accessed by any method inside the class.