类中定义的函数有两大类(3小种)用途,一类是绑定方法,另外一类是非绑定方法

1. 绑定方法:
特点:绑定给谁就应该由谁来调用,谁来调用就会将谁当作第一个参数自动传入
1.1 绑定给对象的:类中定义的函数默认就是绑定对象的
1.2 绑定给类的:在类中定义的函数上加一个装饰器classmethod 2. 非绑定方法
特点: 既不与类绑定也不与对象绑定,意味着对象或者类都可以调用,但无论谁来调用都是一个普通函数,根本没有自动传值一说
class Foo:
def func1(self):
print('绑定给对象的方法',self) @classmethod
def func2(cls):
print('绑定给类的方法: ',cls) @staticmethod
def func3():
print('普通函数')
#
#
# obj=Foo() # obj.func1()
# print(obj) # Foo.func2() # 绑定方法
# print(obj.func1)
# print(Foo.func2) #非绑定方法
# print(obj.func3)
# print(Foo.func3) import settings class Mysql:
def __init__(self,ip,port):
self.id=self.create_id()
self.ip=ip
self.port=port def tell_info(self):
print('%s:%s:%s' %(self.id,self.ip,self.port)) @classmethod
def from_conf(cls):
return cls(settings.IP, settings.PORT) @staticmethod
def create_id():
import uuid
return uuid.uuid4() # obj=Mysql('1.1.1.1',3306)
# obj.tell_info() obj1=Mysql.from_conf() obj1.tell_info()

11、classmethod和staticmethod的更多相关文章

  1. python的@classmethod和@staticmethod

    本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象 ...

  2. classmethod和staticmethod

    假设有这么一个 class class Date(object): def __init__(self, day=0, month=0, year=0): self.day = day self.mo ...

  3. @classmethod和@staticmethod修饰符

    @classmethod和@staticmethod 一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直 ...

  4. python基础知识讲解——@classmethod和@staticmethod的作用

    python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...

  5. Python中的classmethod与staticmethod

    首先,这是一个经典的问题. 我们首先做一个比较: classmethod的第一个参数是cls,即调用的时候要把类传入 这意味着我们我们可以在classmethod里使用类的属性,而不是类的实例的属性( ...

  6. classmethod 和 staticmethod

    我一般很少用到. Talk is cheap, show you the code. #!/usr/bin/env python # -*- coding: utf-8 -*- ########### ...

  7. 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod

    定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...

  8. python基础-abstractmethod、__属性、property、setter、deleter、classmethod、staticmethod

    python基础-abstractmethod.__属性.property.setter.deleter.classmethod.staticmethod

  9. 【python】Python 中的 classmethod 和 staticmethod

    Python 中的 classmethod 和 staticmethod 有什么具体用途? 推荐地址:http://www.cnblogs.com/wangyongsong/p/6750454.htm ...

  10. python classmethod 和 staticmethod的区别

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

随机推荐

  1. 让rm命令提示确认后再删除

    首先在~/.bashrc文件中添加一行: # User specific aliases and functionsalias rm='rm -i' 注意,此处 rm 和 = 之间不能有空格,否则会有 ...

  2. jquery ajax生成Select

    function DropDownList(url, domId, defaultValue) {    /// <summary>    /// ajax生成select    /// ...

  3. VC++ 创建自己的头文件包含GUID

     Guidgen 是比较好用的工具之一,可以使用它来创建ActiveX控件的UUID,同时可以创建独特的剪贴板格式名称和唯一的标识符的内核对象, 如信号量.互斥和事件. 但我需要为另一个目的创建一个唯 ...

  4. insmod 内核模块参数传递

    对于如何向模块传递参数,Linux kernel 提供了一个简单的框架.其允许驱动程序声明参数,并且用户在系统启动或模块装载时为参数指定相应值,在驱动程序里,参数的用法如同全局变量. 通过宏modul ...

  5. jfinal如何调用存储过程?

    存储过程用一下 Db.execute(ICallback) 这个方法,在其中用一下:connection.prepareCall(sql).execute();就可以调用存储过程了,并且还可以自由控制 ...

  6. LeetCode 笔记系列四 Remove Nth Node From End of List

    题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Gi ...

  7. js 中的变量声明提前总结

    一.var 声明 ES6之前,js 中声明变量基本上用 var 关键字: 1.如果访问未声明的变量,会报错:ReferenceError 2.声明了未赋值,值为 undefined,跟前面的报错是两回 ...

  8. js 高程 函数节流 throttle() 分析与优化

    在 js 高程 22.3.3章节 里看到了 函数节流 的概念,觉得给出的代码可以优化,并且概念理解可以清晰些,所以总结如下: 先看 函数节流 的定义,书上原话(斜体表示): 产生原因/适用场景: 浏览 ...

  9. oracle导入导出 dmp文件

    oracle导入导出 dmp文件: 打开cmd窗口,在cmd窗口下,按照个人需要输入以下对应的命令: 1.imp 用户名/密码@网络服务名 file=XXX.dmp fromuser=XXX tous ...

  10. Ningx代码研究.

    概述 研究计划 参与人员 研究文档 学习emiller的文章 熟悉nginx的基本数据结构 nginx 代码的目录结构 nginx简单的数据类型的表示 nginx字符串的数据类型的表示 内存分配相关 ...