python Programming Common and Class and Static Method Examples Detailed Explanation

  • 2021-12-05 06:56:28
  • OfStack

Directory preface runtime environment common method class method static method

Preface

This paper mainly describes three common methods in python class, common method, class method and static method.
This article mainly refers to https://youtu.be/rq8cL2XMM5M. It is highly recommended to watch all the videos in this series.

Running environment


import sys
sys.version

The result is

'3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]'

Common method

Here we define a class called a student, in which we define a common method total_score () to get the total score of a student in an instance.


class Student(object):
    num_of_stu = 0 # Number of students 
    def __init__(self, name, age, math, Chinese):
        self.name = name # Student name 
        self.age = age # Student age 
        self.math = math # Mathematics scores 
        self.Chinese = Chinese # Chinese achievement 
        Student.num_of_stu += 1 # Per instantiation 1 Students, number plus 1 Which is equivalent to a static variable 
    def __del__(self):
        Student.num_of_stu -= 1 # Every deletion 1 Examples, the number of people decreased 1
    # A common method for calculating a student's total score 
    def total_score(self):
        return self.math + self.Chinese

Then we generate several instances and try to see it in 1


print (Student.num_of_stu)
stu1 = Student('Bob', 11, 51, 33)
print (stu1.total_score())
stu2 = Student('Peco', 12, 98, 79)
print (stu2.total_score())
print (Student.num_of_stu)
del stu1
print (Student.num_of_stu)

The result is

0
84
177
2
1

Class method

Now suppose we want to instantiate an instance with a string, then we can add a class method from_string


class Student(object):
    num_of_stu = 0 # Number of students 
    def __init__(self, name, age, math, Chinese):
        self.name = name # Student name 
        self.age = age # Student age 
        self.math = math # Mathematics scores 
        self.Chinese = Chinese # Chinese achievement 
        Student.num_of_stu += 1 # Per instantiation 1 Students, number plus 1 Which is equivalent to a static variable 
    def __del__(self):
        Student.num_of_stu -= 1 # Every deletion 1 Examples, the number of people decreased 1
    # A common method for calculating a student's total score 
    def total_score(self):
        return self.math + self.Chinese
    # Class method for generating an instance with a string 
    @classmethod
    def from_string(cls, stu_str):
        name, age, math, Chinese = stu_str.split('-')
        return cls(name, int(age), float(math), float(Chinese))

Let's have a try


stu_str = "Bob-12-50-34"
stu1 = Student.from_string(stu_str)
print (stu1.name, stu1.total_score())

The result is

Bob 84.0

Static method

Now suppose that we need a method in the class to help us see if it is a class day, then we need a static method


class Student(object):
    num_of_stu = 0 # Number of students 
    def __init__(self, name, age, math, Chinese):
        self.name = name # Student name 
        self.age = age # Student age 
        self.math = math # Mathematics scores 
        self.Chinese = Chinese # Chinese achievement 
        Student.num_of_stu += 1 # Per instantiation 1 Students, number plus 1 Which is equivalent to a static variable 
    def __del__(self):
        Student.num_of_stu -= 1 # Every deletion 1 Examples, the number of people decreased 1
    # A common method for calculating a student's total score 
    def total_score(self):
        return self.math + self.Chinese
    # Class method for generating an instance with a string 
    @classmethod
    def from_string(cls, stu_str):
        name, age, math, Chinese = stu_str.split('-')
        return cls(name, int(age), float(math), float(Chinese))
    # Static method for judging whether to go to school or not 
    @staticmethod
    def is_school_day(day):
        if day.weekday() == 5 or day.weekday() == 6:
            return False
        return True

Let's try it


import datetime
my_date1 = datetime.date(2017, 9, 15)
my_date2 = datetime.date(2017, 9, 16)
print (Student.is_school_day(my_date1))
print (Student.is_school_day(my_date2))

The result is

True
False

The above is the python programming common and class and static method example details, more information about python method please pay attention to other related articles on this site!


Related articles: