11、classmethod和staticmethod
类中定义的函数有两大类(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的更多相关文章
- python的@classmethod和@staticmethod
本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象 ...
- classmethod和staticmethod
假设有这么一个 class class Date(object): def __init__(self, day=0, month=0, year=0): self.day = day self.mo ...
- @classmethod和@staticmethod修饰符
@classmethod和@staticmethod 一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直 ...
- python基础知识讲解——@classmethod和@staticmethod的作用
python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...
- Python中的classmethod与staticmethod
首先,这是一个经典的问题. 我们首先做一个比较: classmethod的第一个参数是cls,即调用的时候要把类传入 这意味着我们我们可以在classmethod里使用类的属性,而不是类的实例的属性( ...
- classmethod 和 staticmethod
我一般很少用到. Talk is cheap, show you the code. #!/usr/bin/env python # -*- coding: utf-8 -*- ########### ...
- 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod
定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...
- python基础-abstractmethod、__属性、property、setter、deleter、classmethod、staticmethod
python基础-abstractmethod.__属性.property.setter.deleter.classmethod.staticmethod
- 【python】Python 中的 classmethod 和 staticmethod
Python 中的 classmethod 和 staticmethod 有什么具体用途? 推荐地址:http://www.cnblogs.com/wangyongsong/p/6750454.htm ...
- python classmethod 和 staticmethod的区别
https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner 1. ...
随机推荐
- cocos2d怎么设置屏幕朝向?横屏 or 竖屏设置
在cocos引擎里面找了好久.没找到相关接口,网上也搜索了好久,最后发现.原来须要依据各个平台分别进行设置. android 改动项目根文件夹 proj.android\AndroidManifest ...
- Python使用pycurl获取http的响应时间
最近需要对节点到源站自己做个监控,简单的ping可以检测到一些东西,但是http请求的检查也要进行,于是就研究了下pycurlpycurl是个用c语言实现的python 库,虽然据说不是那么pytho ...
- ASP.NET MVC - The view must derive from WebViewPage, or WebViewPage<TModel>
当通过一个空的站点构建ASP.NET MVC时经常会出现各种配置缺少的问题,最简单但的办法是吧VS自动生成的web.config文件拷贝到对应的目录下面 The view must derive fr ...
- Spring_day04--SSH框架整合过程
SSH框架整合过程 第一步 导入jar包 第二步 搭建struts2环境 (1)创建action,创建struts.xml配置文件,配置action (2)配置struts2的过滤器 第三步 搭建hi ...
- Qt Creator开发的程序提升到管理员权限运行
一些功能需要管理员权限,例如启动一个服务,这就需要exe在管理员权限下运行,一个方法是在exe上右键,选择“以管理员身份运行”,或者右键-属性-兼容性-勾选“以管理员身份运行此程序” 另一个方法是在程 ...
- poj_2823 单调队列
题目大意 给定一行数,共N个.有一个长度为K的窗口从左向右滑动,窗口中始终有K个数字,窗口每次滑动一个数字.求各个时刻窗口中的最大值和最小值. 题目分析 直接搜索,复杂度为O(n^2).考虑使用单调队 ...
- 以打字形式展示placeholder的插件
http://weber.pub/以打字形式展示placeholder的插件/197.html
- highcharts配置的效果如下
配置如下: function init(categoryArray,seriesData,month_first_day,month_last_day,currDay){ var chart = Hi ...
- java读代码步骤
一.读代码的步骤 1.知道代码时用什么IDE开发的 2.将代码导入到IDE 3.连接数据库 A)连接到测试数据库 B)有sql脚本,在本地创建一个数据库,执行脚本,建立数据结构和导入数据. 4.尝试运 ...
- Oracle Schema Objects(Schema Object Storage And Type)
One characteristic of an RDBMS is the independence of physical data storage from logical data struct ...