Python static method code snippets

Posted by

Hello Coders,

We have discussed the python static method on the Youtube channel in detail. If you go through this, you will have a clear understanding of the static method, and also it will help to crack the interviews.

Below is the concept we have covered.

  1. What is the static method?
  2. A basic example of the static method.
  3. Why static method consumes less memory than the regular method?
  4. How to call a static method from another method?
  5. How to create a utility function using the static method?
  1. What is the static method? Find the video Link here.

2. Let’s see a basic static method example. Find the Video Link Here.

# basic example

class Student:

    @staticmethod
    def marks(x)
        print(f"mark of the student {x}")
Student.marks(30)

mark of the student 30

3. Why static method consumes less memory than the regular method? Find the video link here
class Mobilephone:

    def __init__(self, brand, price):

        self.brand = brand

        self.price = price

   

    def display_information(self):

        print(f"brand {self.brand} price {self.price}")

   

    @staticmethod

    def just_a_method():

        print("this is just a static method")

mobile_1 = Mobilephone("Apple",60000)

mobile_2 = Mobilephone("Samsumg",55000)
mobile_1.display_information
<bound method Mobilephone.display_information of <__main__.Mobilephone object at 0x000001175B019100>>
mobile_2.display_information
<bound method Mobilephone.display_information of <__main__.Mobilephone object at 0x000001175ADF1640>>
Mobilephone.just_a_method
<function __main__.Mobilephone.just_a_method()>
mobile_1.just_a_method
<function __main__.Mobilephone.just_a_method()>
4. How to call a static method from another method? Link here
class Y:

   

    @staticmethod

    def static_method_1():

            print("static method 1")

   

    @staticmethod

    def static_method_2():

            print("static method 2")

    @classmethod

    def class_method(cls):

        print("calling from class method ....")

        Y.static_method_1()

Y.class_method()

calling from class method ….
static method 1

5. How to create a utility function using the static method?  Link Here

class Car:

    def __init__(self,brand,price):

        self.brand = brand

        self.price = price

   

    def display(self):

        print(f"brand: {self.brand}, factory price {self.price}")

        print(f"onroad price of the car: {Car.on_roadprice(self.price)}")

   

    def load_amount(self):

        load_required = Car.on_roadprice(self.price) * 0.3

        print(f"customer need to pay min of {load_required}")

   

    @staticmethod

    def on_roadprice(price):

        return price + price * 0.3

       

       

car_1 = Car("BMW",5000000)

car_1.display()

car_1.load_amount()
brand: BMW, factory price 5000000
onroad price of the car: 6500000.0
customer need to pay min of 1950000.0

Leave a Reply

Your email address will not be published. Required fields are marked *