类中定义的函数有两大类(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. Cocos2d-X中的Slider控件

    Slider控件事实上就是滑块控件.经常使用于音乐中的音量控制,在Windows编程中开发音乐播放器就须要用到滑块控件控制音量 首先在project文件夹下的Resource文件夹中放 在Skider ...

  2. pychram最新注册码

    2016年的激活码只能用到2017.2.25,于昨日已经过期了. 现提供最新激活码: BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZW ...

  3. 编程之美 set 5 寻找数组中最大值和最小值

    解法 1. 设置 min, max 两个变量, 然后遍历一遍数组, 比较次数为 2*N 2. 依然设置 min, max 两个变量并遍历数组, 但将遍历的 step 设置为 2, 比较次数为 1.5 ...

  4. mac 10.9 dock在多屏幕间移动

    想要在哪个屏幕使用dock,就在这个屏幕把鼠标移动到最底部即可.神奇吧?太意外了...居然被我发现了...

  5. 应用开发之WinForm环境

    本章简言 上一章笔者讲到关于IO文件操作类,了解如何处理文件流.从这一章开始笔者将讲解相对比较高级的知识点.而本章笔者就对WinForm开发的知识点进行讲解和引导.现在很多业务都是面向于B/S模式的开 ...

  6. 关于recycler遇到的问题

    1.//设置recyclerView不能点击myLayoutManager.setScrollEnabled(false);class MyLayoutManager extends LinearLa ...

  7. phpcms v9表单实现问答咨询功能

    本文转自别人 phpcms v9的留言板插件可以安装留言板,做问答咨询,那样的话有很多东西需要修改,也有人发现phpcms v9有个表单向导功能,只能留言,不能回复,今天仿站网:新源网络工作室告诉大家 ...

  8. std::condition_variable(二)

    #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::yie ...

  9. struts2中s:iterator 标签的使用详解 及 OGNL用法

    简单的demo: s:iterator 标签有3个属性:value:被迭代的集合id   :指定集合里面的元素的idstatus 迭代元素的索引 1:jsp页面定义元素写法 数组或list <s ...

  10. angular4 *ngFor获取index

    angular 中的*ngFor指令的使用 <ul> <li *ngFor="let item in items">{{item}}</li> ...