Tuesday 25 June 2024

5 Levels of Writing Python Classes

 

Level 1: Basic Class Creation and Instantiation

# Defining a basic class

class Dog:

    def __init__(self, name, age):

        self.name = name

        self.age = age


# Creating an instance

my_dog = Dog('Buddy', 3)


# Accessing attributes

print(my_dog.name)  

print(my_dog.age)   


#clcoding.com

Buddy

3

Level 2: Methods and Instance Variables

# Defining a class with methods

class Dog:

    def __init__(self, name, age):

        self.name = name

        self.age = age


    def bark(self):

        print(f"{self.name} says woof!")


# Creating an instance and calling a method

my_dog = Dog('Buddy', 3)

my_dog.bark()  


#clcoding.com

Buddy says woof!

Level 3: Class Variables and Class Methods

# Defining a class with class variables and methods

class Dog:

    species = 'Canis familiaris'  # Class variable


    def __init__(self, name, age):

        self.name = name

        self.age = age


    def bark(self):

        print(f"{self.name} says woof!")


    @classmethod

    def get_species(cls):

        return cls.species


# Accessing class variables and methods

print(Dog.species)  

print(Dog.get_species())  

#clcoding.com

Canis familiaris

Canis familiaris

Level 4: Inheritance and Method Overriding

# Base class and derived classes

class Animal:

    def __init__(self, name):

        self.name = name


    def speak(self):

        raise NotImplementedError("Subclasses must implement this method")


class Dog(Animal):

    def speak(self):

        return f"{self.name} says woof!"


class Cat(Animal):

    def speak(self):

        return f"{self.name} says meow!"


# Instances of derived classes

my_dog = Dog('Buddy')

my_cat = Cat('Whiskers')


# Calling overridden methods

print(my_dog.speak())  

print(my_cat.speak())  

#clcoding.com

Buddy says woof!

Whiskers says meow!

Level 5: Advanced Features (Polymorphism, Abstract Base Classes, Mixins)

from abc import ABC, abstractmethod


# Abstract base class

class Animal(ABC):

    @abstractmethod

    def speak(self):

        pass


# Derived classes implementing the abstract method

class Dog(Animal):

    def __init__(self, name):

        self.name = name


    def speak(self):

        return f"{self.name} says woof!"


class Cat(Animal):

    def __init__(self, name):

        self.name = name


    def speak(self):

        return f"{self.name} says meow!"


# Polymorphism in action

def animal_speak(animal):

    print(animal.speak())


# Creating instances

my_dog = Dog('Buddy')

my_cat = Cat('Whiskers')


# Using polymorphism

animal_speak(my_dog)  

animal_speak(my_cat)  

Buddy says woof!

Whiskers says meow!

 

0 Comments:

Post a Comment

Popular Posts

Categories

AI (28) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (121) C (77) C# (12) C++ (82) Course (64) Coursera (182) Cybersecurity (24) data management (11) Data Science (99) Data Strucures (6) Deep Learning (11) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Pandas (3) PHP (20) Projects (29) Python (775) Python Coding Challenge (260) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses