实例方法:在类中,定义的方法,这个方法的第一个参数默认是实例对象,一般习惯使用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()

classmethod和staticmethod区别的更多相关文章

  1. python @classmethod和@staticmethod区别

    python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...

  2. Python中classmethod与staticmethod区别

    classmethod:类方法staticmethod:静态方法 在python中,静态方法和类方法都是可以通过类对象和类对象实例访问.但是区别是: @classmethod 是一个函数修饰符,它表示 ...

  3. Python的classmethod和staticmethod区别

    静态方法(staticmethod) 类方法(classmethod) 静态方法和类方法都可以通过类名.方法名或者实例.方法访问. #-*- coding:utf8 -*- class A(objec ...

  4. @classmethod 与 @staticmethod 区别

  5. python(三)@staticmethod和@classmethod使用和区别

    转载自[1] 一般要用某个类的方法,先要实例这个类. 但是可以通过@staticmethod和@classmethod,直接用“类.方法()”来调用这个方法. 而 @staticmethod和@cla ...

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

    python中的定义: class MyClass: ... @classmethod  # classmethod的修饰符 def class_method(cls, arg1, arg2, ... ...

  7. 面试题:python 中 staticmethod 和 classmethod有什么区别

    面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...

  8. python 中 staticmethod 和 classmethod有什么区别

    面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...

  9. python classmethod 和 staticmethod的区别

    https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner 1. ...

随机推荐

  1. C#中基本类型占用字节数

    bool -> System.Boolean (布尔型,其值为 true 或者 false) byte -> System.Byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ...

  2. PHP使用CURL获取302跳转后的地址实例

    /*返回一个302地址*/     function  curl_post_302($url, $vars) { $ch = curl_init();          curl_setopt($ch ...

  3. A - Bi-shoe and Phi-shoe 欧拉函数

    /** 题目:A - Bi-shoe and Phi-shoe 链接:https://vjudge.net/contest/154246#problem/A 题意:每一个数都有一个得分,它的得分就是, ...

  4. 初尝CDN:什么是分布式服务节点?

    什么是CDN?笔者初见时也是摸不着头脑,查阅了大量的资料才明白什么是CDN,笔者为您揭秘什么是CDN? CDN的全称是Content Delivery Network,即内容分发网络.CDN的通俗理解 ...

  5. Servlet 处理日期

    使用 Servlet 的最重要的优势之一是,可以使用核心 Java 中的大多数可用的方法.本章将讲解 Java 提供的 java.util 包中的 Date 类,这个类封装了当前的日期和时间. Dat ...

  6. java中Statement详细用法。

    1.创建 Statement 对象 建立了到特定数据库的连接之后,就可用该连接发送 SQL 语句.Statement 对象用 Connection 的方法createStatement 创建,如下列代 ...

  7. Eclipse 运行配置(Run Configuration)

    Eclipse 运行配置(Run Configuration) 创建和使用 Eclipse 运行配置 在运行配置(Run Configuration)对话框中可以创建多个运行配置.每个配置可以在应用中 ...

  8. sourcenav安装

    $ ./configure之后会出现 configure: error: ./configure failed for unixconfigure: error: ./configure failed ...

  9. SQL金典

    ps:补充自己的基础知识,大神请无视.. ~~~~~~~~~~~~~~~~~~~~~ DataBase Management System,DBMS.... Catalog ...库 Table... ...

  10. go语言获取字符串元素的个数

    1:获取字符串字节的个数,并按字节挨个输出 package main import ( "fmt" ) func main() { var str string = "a ...