Shawn's blog Shawn's blog
About Me
  • Category
  • Tag
  • Archive
GitHub (opens new window)

Shawn Jin

I am not a creator of knowledge, I am just a porter of knowledge.
About Me
  • Category
  • Tag
  • Archive
GitHub (opens new window)
  • Python

    • Python import files from different directories
    • Single/Double Star (*/**) Parameters in Python
    • Classmethod in Python
      • Class method vs. Static Method
    • Python modify range variable in for loop
  • Git

  • Notes
  • Python
Shawn Jin
2021-09-15

Classmethod in Python

# Classmethod in Python

The classmethod() is an inbuilt function in Python, which returns a class method for a given function.

# fun: the function that needs to be convert into a class method
# return: a class method for function
@classmethod
def fun(cls, arg1, arg2, ...):
	"""
	cls: class
	"""
1
2
3
4
5
6
7

# Class method vs. Static Method

  • A class method takes cls as the first parameter while a static method needs no specific parameters.
  • A class method can access or modify the class state while a static method can’t access or modify it.
  • In general, static methods know nothing about the class state. They are utility-type methods that take some parameters and work upon those parameters. On the other hand class methods must have class as a parameter.
  • We use @classmethod decorator in python to create a class method and we use @staticmethod decorator to create a static method in python.
#P#y#t#h#o#n
Updated: 2021/09/15, 21:07:45
Single/Double Star (*/**) Parameters in Python
Python modify range variable in for loop

← Single/Double Star (*/**) Parameters in Python Python modify range variable in for loop→

最近更新
01
Python import files from different directories
12-31
02
Single/Double Star (/*) Parameters in Python
09-15
03
Backprop Algorithm in Machine Learning
09-08
更多文章>
Theme by Vdoing | Copyright © 2019-2021 Shawn Jin | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式