Instance method, Class method, and Static method

class Student:
# initialization method
def __init__(self, first, last):
#instance variable
self.first = first
self.last = last

# instance class
def get_detail(self, mark):
return self.first + " " + self.last + " " + str(mark)

stud = Student("Ram", "Kumar")

print(stud.get_detail(70))
Ram Kumar 70
class Student:
#class variable
percent = 10
# initialization method
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age

# instance method
def mark(self, mark, percent):
self.mark = mark
self.percent = percent

# class method
@classmethod
def change_per(cls, percent):
cls.percent = percent + percent

stud = Student("Ram", "Kumar", 50)
#calling class variable
Student.change_per(50)
stud.mark(100, Student.percent)
print(stud.__dict__)
{‘first’: ‘Ram’, ‘last’: ‘Kumar’, ‘age’: 50, ‘mark’: 100, ‘percent’: 100}
class Student:
#class variable
percent = 10
# initialization method
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age

# instance method
def mark(self, mark, percent):
self.mark = mark
self.percent = percent

# Static method
@staticmethod
def get_age(age):
return age

stud = Student("Ram", "Kumar",Student.get_age(50))
#calling class variable
stud.mark(100, Student.percent)
print(stud.__dict__)
{'first': 'Ram', 'last': 'Kumar', 'age': 50, 'mark': 100, 'percent': 10}
class Student:
percent = 10
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age

def mark(self, mark, percent):
self.mark = mark
self.percent = percent

@classmethod
def change_per(cls, percent):
cls.percent = percent + percent

@staticmethod
def get_age(age):
return age


stud = Student("Hello", "World", Student.get_age(50))
Student.change_per(20)
stud.mark(100, Student.percent)
print(stud.__dict__)
{'first': 'Hello', 'last': 'World', 'age': 50, 'mark': 100, 'percent': 40}

--

--

--

Network Automation Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The case of Mysterious System Resets on Mars Pathfinder

Legacy IT Systems — just deal with it?

Become a Better Developer by Surrounding Yourself with the Right People

My second cup of Rust

Structured logging in third-party .NET logging frameworks

10 Engineering Skills That Help Bridge the Gap Between Bootcamp and Professional

A CRAH COURSE IN PYTHON PART 2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dineshbaburam

Dineshbaburam

Network Automation Engineer

More from Medium

Making Sense of SQL: Part 3

ER Diagram to Create Database Schema Made Simple: What You Need to Know

Go nulls and SQL

Stack: Data Structure