A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python-classes-and-objects/ below:

Python Classes and Objects - GeeksforGeeks

Python Classes and Objects

Last Updated : 14 Aug, 2025

Python’s object-oriented programming (OOP) allows you to model real-world entities in code, making programs more organized, reusable and easier to maintain. By grouping related data and behavior into a single unit, classes and objects help you write cleaner, more logical code for everything from small scripts to large applications.

Let’s explore how classes and objects work in Python and see them in action with simple examples.

Class

A class in Python is a user-defined template for creating objects. It bundles data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type.

Classes and Objects (Here Dog is the Base Class and Bobby is Object) Creating Class

Classes are created using class keyword. Attributes are variables defined inside class and represent properties of the class. Attributes can be accessed using dot . operator (e.g., MyClass.my_attribute).

Python
# define a class
class Dog:
    sound = "bark"  # class attribute
Object

An object is a specific instance of a class. It holds its own set of data (instance variables) and can invoke methods defined by its class. Multiple objects can be created from same class, each with its own unique attributes.

Let's create an object from Dog class.

Python
class Dog:
    sound = "bark"

dog1 = Dog() # Creating object from class
print(dog1.sound) # Accessing the class

Explanation: sound attribute is a class attribute. It is shared across all instances of Dog class, so can be directly accessed through instance dog1.

Why do we need Classes and Objects Using __init__() Function

In Python, class has __init__() function which automatically initializes object attributes when an object is created. The __init__() method is the constructor in Python.

Let’s see how to create a class with __init__() to set up attributes when an object is made:

Python
class Dog:
    species = "Canine"  # Class attribute

    def __init__(self, name, age):
        self.name = name  # Instance attribute
        self.age = age  # Instance attribute

Explanation:

Initiate Object with __init__()

Let’s now create an object using __init__() method and see how it initializes attributes:

Python
class Dog:
    species = "Canine"  # Class attribute

    def __init__(self, name, age):
        self.name = name  # Instance attribute
        self.age = age  # Instance attribute

# Creating an object of the Dog class
dog1 = Dog("Buddy", 3)

print(dog1.name)  
print(dog1.species)  
Explanation: Self Parameter

self parameter is a reference to the current instance of class. It allows us to access the attributes and methods of the object.

Let’s see an example of how the self parameter works in accessing attributes and methods:

Python
class Dog:
    def __init__(self, name, age):  
        self.name = name 
        self.age = age

    def bark(self): 
        print(f"{self.name} is barking!")

# Creating an instance of Dog
dog1 = Dog("Buddy", 3)
dog1.bark() 

Explanation:

__str__() Method

__str__ method in Python allows us to define a custom string representation of an object. By default, when we print an object or convert it to a string using str(), Python uses the default implementation, which returns a string like <__main__.ClassName object at 0x00000123>.

Let’s look at an example of using __str__() method to provide a readable string output for an object:

Python
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"{self.name} is {self.age} years old."
dog1 = Dog("Buddy", 3)
dog2 = Dog("Charlie", 5)

print(dog1)  
print(dog2)  

Explanation:

Class and Instance Variables in Python

In Python, variables defined in a class can be either class variables or instance variables and understanding distinction between them is crucial for object-oriented programming.

Class Variables

These are variables that are shared across all instances of a class. It is defined at class level, outside any methods. All objects of class share same value for a class variable unless explicitly overridden in an object.

Instance Variables

Variables that are unique to each instance (object) of a class. These are defined within __init__() method or other instance methods. Each object maintains its own copy of instance variables, independent of other objects.

Example:

This code shows how class variables are shared across all objects, while instance variables are unique to each object.

Python
class Dog:
    # Class variable
    species = "Canine"

    def __init__(self, name, age):
        # Instance variables
        self.name = name
        self.age = age

# Create objects
dog1 = Dog("Buddy", 3)
dog2 = Dog("Charlie", 5)

# Access class and instance variables
print(dog1.species)  # (Class variable)
print(dog1.name)     # (Instance variable)
print(dog2.name)     # (Instance variable)

# Modify instance variables
dog1.name = "Max"
print(dog1.name)     # (Updated instance variable)

# Modify class variable
Dog.species = "Feline"
print(dog1.species)  # (Updated class variable)
print(dog2.species)  
Explanation: Additional Important Concepts in Python Classes and Objects 1. Getter and Setter Methods

Getter and Setter methods provide controlled access to an object's attributes. In Python, these methods are used to retrieve(getter) or modify(setter) values of private attributes, allowing for data encapsulation.

Python doesn't have explicit get and set methods like other languages, but it supports this functionality using property decorators.

Let’s see an example of implementing getter and setter methods using @property decorators:

Python
class Dog:
    def __init__(self, name, age):
        self._name = name  # Conventionally private variable
        self._age = age  # Conventionally private variable

    @property
    def name(self):
        return self._name  # Getter

    @name.setter
    def name(self, value):
        self._name = value  # Setter

    @property
    def age(self):
        return self._age  # Getter

    @age.setter
    def age(self, value):
        if value < 0:
            print("Age cannot be negative!")
        else:
            self._age = value  # Setter

Explanation:

2. Method Overriding

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. This allows subclasses to modify or extend behavior of inherited methods.

Let’s look at an example of method overriding, where a subclass changes the behavior of an inherited method:

Python
class Animal:
    def sound(self):
        print("Some sound")

class Dog(Animal):
    def sound(self):  # Method overriding
        print("Woof")

dog = Dog()
dog.sound()  

Explanation:

3. Static Methods and Class Methods

Static methods and class methods are bound to the class, not instances of the class.

Let’s see how to define and use @staticmethod and @classmethod in Python:

Python
class Dog:
    @staticmethod
    def info():
        print("Dogs are loyal animals.")

    @classmethod
    def count(cls):
        print("There are many dogs of class", cls)

dog = Dog()
dog.info()  # Static method call
dog.count()  # Class method call

Output
Dogs are loyal animals.
There are many dogs of class <class '__main__.Dog'>

Explanation:

4. Abstract Classes and Interfaces

Abstract classes provide a template for other classes. These classes can't be instantiated directly. They contain abstract methods, which are methods that must be implemented by subclasses.

Abstract classes are defined using abc module in Python. Let’s see an example of using an abstract class to define a required method for subclasses:

Python
from abc import ABC, abstractmethod
class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        print("Woof")

dog = Dog()
dog.sound()  

Explanation:

5. Class Variables vs. Instance Variables

Let’s see difference between a class variable and an instance variable in Python:

Python
class Dog:
    species = "Canine"  # Class variable
    
    def __init__(self, name, age):
        self.name = name  # Instance variable
        self.age = age  # Instance variable

dog1 = Dog("Buddy", 3)
dog2 = Dog("Lucy", 2)

print(dog1.species)  # Accessing class variable
print(dog2.name)  # Accessing instance variable

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4