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. ...
随机推荐
- linux -- ubuntuserver 安装图形界面
安装Gnome桌面 1.安装全部桌面环境,其实Ubuntu系列桌面实际上有几种桌面应用程序,包括Ubuntu-desktop.Kubunut-desktop和Xubuntu- desktop. 我们就 ...
- 插件之下拉框Select2
select2为代替常规的select而出现,可自定义select的样式,最明显的功能就是集合中可以搜索 关于浏览器要求,ie8+,Chrome 8+,Firefox 10+,Safari 3+,Op ...
- C++关键字之explicit(显式)
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...
- sublime window 配置记录 (转)
大家好,今天给大家分享一款编辑器:sublime text2 我用过很多编辑器,EditPlus.EmEditor.Notepad++.Notepad2.UltraEdit.Editra.Vim ...
- js函数柯里化
function curry(fn){ // 代码 } function add(a,b,c){ return a + b + c; } const execAdd = curry(add); exe ...
- Spring_day03--Spring配置c3p0连接池和dao使用jdbcTemplate
Spring配置c3p0连接池和dao使用jdbcTemplate 1 spring配置c3p0连接池 第一步 导入jar包 第二步 创建spring配置文件,配置连接池 原始方式 (1)把代码在配置 ...
- PagerAdapter 普通写法
1,viewPagre的普通写法 public ImagePagerAdapter(Context context, List<Photo> imgList) { this.mContex ...
- 利用CSS3制作淡入淡出动画效果
CSS3新增动画属性“@-webkit-keyframes”,从字面就可以看出其含义——关键帧,这与Flash中的含义一致. 利用CSS3制作动画效果其原理与Flash一样,我们需要定义关键帧处的状态 ...
- SNORT--install ---dependency-resolve
# ./configure 遇到ERROR: checking for pfring_open in -lpcap... no ERROR! Libpcap library/headers (lib ...
- json和jsonp解决跨域传输数据等
https://kb.cnblogs.com/page/139725/ https://blog.csdn.net/lambert310/article/details/51683775