Static Methods

Syntax:

    @staticmethod
def print_good(string):
        print("This is good " + string)
print(karan.print_good("Bala"))
Ouput:
This is good Bala
None

Code: 

class Employee:

    no_of_leaves = 89

def __init__(self, aname, asalary, arole):
self.name = aname
self.salary = asalary
self.role = arole

def printdetails(self):
return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"

@classmethod
def change_leaves(cls, newleaves):
cls.no_of_leaves = newleaves

@classmethod
def from_str(cls, string):
return cls(*string.split("-"))

@staticmethod
def print_good(string):
print("This is good " + string)


harry = Employee("Harry", 255, "Instructor")
rohan = Employee("Rohan", 455, "Student")
karan = Employee.from_str("Karan-480-Student")

print(karan.print_good("Bala"))

Comments

Popular posts from this blog

Practice Execise 3

Pickle Module