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. ...
随机推荐
- chmod 4777? 文件特殊权限 SUID SGID StickyBit
故事引入 今天碰到了一条指令, test 怎么在777前还有一位,颠覆了我的认知啊,这时候必须翻鸟哥神书了,找到一个链接<7.4.3 文件特殊权限:SUID/SGID/Sticky Bit> ...
- Hibernate_day03--Hibernate多对多操作
Hibernate多对多操作 多对多映射配置 以用户和角色为例演示 第一步 创建实体类,用户和角色 第二步 让两个实体类之间互相表示 (1)一个用户里面表示所有角色,使用set集合 具体: User. ...
- iOS开发之 -- bundle程序束的制造
我们在写项目的时候,需要添加大量的图片,这个时候除了在x-code-->Assets文件里面添加图片外,还可以添加程序束,这样的话 项目看起来比较整齐,也显得比较专业,下面就来说一下程序束的制造 ...
- Python tushare 初步了解
一.安装 1.Python 2.numpy 3.pandas 4.lxml 5.............. n.tushare 二.初步测试
- UE4中VR模式下窗口单目双目的问题
如果要是单目的话使用HMD MIRROR MODE 3或者4
- 自定义控件_StickyNavLaout
关注我一.View结构原理1.extends linearLayout 继承想要用的布局,首先完成布局的填充在 onFinishInflate 方法中 findViewById(); @Overrid ...
- Spring学习笔记--使用注解装配
使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...
- js小功能实现
发送随机数手机验证码60秒倒计时 mm.mobileCheck = function(t){ var mobile = $("#user_mobile").val(); if(&q ...
- 【BZOJ5083】普及 单调栈+二分+RMQ
[BZOJ5083]普及 Description 有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(从左到右或从右到左一个一个取出),使得 不管是从左往右还是从右往左取,都保证每时每刻 ...
- 数组和对象常用API
数组API: 1. forEach 遍历所有元素 var arr = [1,2,3] arr.forEach(function(item,index){ // 遍历数组的所有元素 console.lo ...