classmethod和staticmethod区别

实例方法:在类中,定义的方法,这个方法的第一个参数默认是实例对象,一般习惯使用self
类方法:在类中,定义的方法,这个方法的第一个参数默认是类对象,一般习惯用cls表示,用@classmethod装饰器装饰
静态方法:在类中定义的方法,这个方法的参数没有要求,用@staticmethod装饰器装饰
实例方法只能被实例(对象)调用
类方法和静态方法可以被类或者实例调用

class Foo(object):  

    # 实例方法,第一个参数必须是实例对象。一般习惯用self。
    def instance_method(self):
        print("是类{}的实例方法,只能被实例对象调用".format(Foo))  

    # 类方法, 第一个参数必须是类 对象。一般习惯使用cls。使用@classmethod装饰器装饰。
    @classmethod
    def class_method(cls):
        print("是类方法")  

    # 静态方法,参数没有要求,和类没有绑定关系,就是一个普通的方法            使用@staticmethod装饰器装饰。
    @staticmethod
    def static_method():
        print("是静态方法")  

foo = Foo()  

# 实例方法只能被实例调用。
foo.instance_method()  

print(‘----------‘)  

# 类方法可以被类或者实例调用。
Foo.class_method()
foo.class_method()  

print(‘----------‘)  

# 静态方法可以被类或者实例调用。
Foo.static_method()
foo.static_method()  

原文地址:https://www.cnblogs.com/z-x-y/p/9169130.html

时间: 2024-08-30 10:54:18

classmethod和staticmethod区别的相关文章

Python中classmethod与staticmethod区别

classmethod:类方法staticmethod:静态方法 在python中,静态方法和类方法都是可以通过类对象和类对象实例访问.但是区别是: @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法. 类方法的第一个参数cls,而实例方法的第一个参数是self,表示该类的一个实例. 普通对象方法至少需要一个self参数,代表类对象实例 类方法有类变量cls传入,从而可以用cls做一些相关的处理.并且有子类继承时,调用该类方法时,传入的类变

Python的classmethod和staticmethod区别

静态方法(staticmethod) 类方法(classmethod) 静态方法和类方法都可以通过类名.方法名或者实例.方法访问. #-*- coding:utf8 -*- class A(object): def instance_method(self,x): print "instance_method (%s,%s)" % (self,x) @classmethod def class_method(cls,x): #类方法的调用使用类本身作为其隐含的参数 #调用者本身并不需要

python @classmethod 和 @staticmethod区别

首先来看@staticmethod,这个装饰器很好理解,就是让类中的方法变成静态方法,不需要对象实例化就可以直接调用.在此静态方法中无法使用"self"参数: 再看@classmethod.其实和@staticmethod功能类似, 可以简单理解为@staticmethod功能上增加了一个"cls"参数代表本类obj(在类继承中,cls代表子类),可以通过调用cls()对象调用本类中的其他对象(python中所有皆是对象). 如果你对以上两句话还不能理解,请看下面的

classmethod和staticmethod区别(转载)

主要classmethod是被类直接调用使用 statifcmethod是在类内部访问时候并且是能被类直接调用时候使用 原文链接地址:click

python -- @classmethod @staticmethod区别和使用

python中的定义: class MyClass: ... @classmethod  # classmethod的修饰符 def class_method(cls, arg1, arg2, ...): ... @staticmethod  # staticmethod的修饰符 def static_method(arg1, arg2, ...): ... @classmethod : 类方法 @staticmethod : 静态方法 类方法和静态方法的调用一样,都是通过类就可以直接调用. 区

Python中@property和@classmethod和@staticmethod

前戏 首先,先要弄清楚一个类里面的,各个组成部分都应该怎么称呼. - 注:可能叫法会不太一样. 关于@property 顾名思义:它的意思为'属性'. 作用: 1:使用它你将会把类方法,变为类属性.并且是只读属性. 2:它会重新实现getter和setter方法. 看代码: class Person: def __init__(self,first_name,last_name): self.first_name = first_name self.last_name = last_name @

python的@classmethod和@staticmethod

本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象编程中,类中定义的方法可以是@classmethod 装饰的类方法,也可以是@staticmethod 装饰的静态方法,用的最多的还是不带装饰器的实例方法.为方便,在下文中用@classmethod装饰的类方法将直接用@classmethod来表述,@staticmethod同理,望读者在阅读时自行

python中classmethod与staticmethod的差异及应用

类中三种函数的应用 #!/usr/bin/env python # -*- coding: utf-8 -*- class TClassStatic(object): def __init__(self, data): self.data = data def printself(*arg): # for item in arg: # print item.data print("printself: ", arg) @staticmethod def smethod(*arg): p

16 Apr 18 封装的property 多态 鸭子类型 classmethod和staticmethod

16 Apr 18 一.      封装的property BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而非方法,如果我们将其做成一个属性,更便于理解) 成人的BMI数值: 过轻:低于18.5 正常:18.5-23.9 过重:24-27 肥胖:28-32 非常肥胖, 高于32 体质指数(BMI)=体重(kg)÷身高^2(m) 首先需要明确.bmi是算出来的,不是一个固定死的值,也就说我们必须编写一个功能,每次调用该功能都会立即计算一个值,但很明显人的bmi值听起来更像一个名词而非