Use Module and Function instead of Class in Python
The following scripts run in ipython demonstrate the differences between instance method and static method.
Generally OOP make things complicated for imperative style applications.
So when developing this style application (such as command-line application) use functions inside modules, instead of "module - class - method".
The Python standard library is a good example that all utility functions are all common functions not wrapped in classes.
In [1]: cat utils.py
class FileUtils:
def __init__(self):
print 'initialize', self
@staticmethod
def static_copy(src, dst):
print 'class copy from', src, 'to', dst
def instance_copy(self, src, dst):
print self, 'copy from', src, 'to', dst
In [2]: import utils
In [3]: myutil = utils.FileUtils()
initialize <utils.FileUtils instance at 0x8ab250c>
In [4]: myutil.instance_copy('aa', 'bb')
<utils.FileUtils instance at 0x8ab250c> copy from aa to bb
In [5]: utils.FileUtils().instance_copy('aa', 'bb')
initialize <utils.FileUtils instance at 0x8ab24ec>
<utils.FileUtils instance at 0x8ab24ec> copy from aa to bb
In [6]: utils.FileUtils.static_copy('aa', 'bb')
class copy from aa to bb
In [7]: utils.FileUtils.instance_copy('aa', 'bb')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-cce9f79fc45b> in <module>()
----> 1 utils.FileUtils.instance_copy('aa', 'bb')
TypeError: unbound method instance_copy() must be called with FileUtils instance as first argument (got str instance instead)
In [8]: myutil.static_copy('aa', 'bb')
class copy from aa to bb
Notice the subtle difference between [5] and [6], parenthesis followed the class name is a "instance", while only class name without the following parenthesis is a "class".
Ref: The definitive guide on how to use static, class or abstract methods in Python
Use Module and Function instead of Class in Python的更多相关文章
- Python: import vs from (module) import function(class) 的理解
Python: Import vs From (module) import function(class) 本文涉及的 Python 基本概念: Module Class import from . ...
- ImportError: dynamic module does not define module export function (PyInit__sqlite3)
使用python3.6 中的django-admin创建项目的时候报错 ImportError: dynamic module does not define module export functi ...
- 编译caffe的Python借口,提示:ImportError: dynamic module does not define module export function (PyInit__caffe)
>>> import caffeTraceback (most recent call last): File "<stdin>", line 1, ...
- Erlang Module and Function
Module -module(Name). 模块是方法的集合.注意这行最后的“.”符号是必不可少的. 这个模块名必须和保存这段代码的文件(后缀为“erl”的文件)有相同的名称. 当我们在使用另一个 ...
- ImportError: dynamic module does not define module export function (PyInit__caffe)
使用python3运行caffe,报了该错误. 参考网址:https://stackoverflow.com/questions/34295136/importerror-dynamic-module ...
- cv2.SIFT() AttributeError: 'module' object has no attribute 'SIFT' OpenCV Python can't use SURF, SIFT
参考链接: https://stackoverflow.com/questions/18561910/opencv-python-cant-use-surf-sift For recent infor ...
- function module 之间调用
1: 在一个function group 中定义一个function module 2:在另外一个module中调用该module "调用其它function 要用 单引号 引着. 一个mo ...
- pytest fixture中scope试验,包含function、module、class、session、package
上图是试验的目录结构 conftest.py:存放pytest fixture的文件 import uuid import pytest @pytest.fixture(scope="mod ...
- nodejs模块中exports和module.exports的区别
通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ ,这些核心模块被编译成二进制文件,可以require('模块名')去获取:核心 ...
随机推荐
- 修改MySQL时区
说明: 1.Windows版本暂无发现问题 2.CentOS-Docker版本需要修改时区 通过sql命令临时修改 mysql> set global time_zone = '+8:00'; ...
- Linux:VMware配置NAT网络IP
设置虚拟机网络配置 在目标虚拟机下右键, 选择"设置", 打开"虚拟机设置"对话框, 再选择"网络适配器"使用NAT模式的, 如下图所示: ...
- 前端集合传参,springmvc后端如何接收
废话不多说,上代码 后端接收对象: class ObjectA{ private String a; private String b; private List<ObjectB> lis ...
- Https:创建部署SSL证书进行双向认证
一.前言 建立客户端与服务器的Https的连接需要证书进行双向验证后,才可访问. 二.证书类型 不同数字证书部署在服务器上后,用户浏览器访问网站时,展示如下: 1.无证书时 显示不安全标识. 2. ...
- IDA Pro 6.0使用Qt 框架实现了跨平台的UI
IDA Pro 6.0使用Qt 框架实现了跨平台的UI.它的好处是插件编写者还可以直接使用 Qt 开发跨平台 UI.但是编剧呢? 在这篇博文中,我们将说明如何使用PySide使用IDAPython为 ...
- 『无为则无心』Python函数 — 28、Python函数的简单应用
目录 1.函数嵌套调用 2.Python函数的简单应用 (1)打印线条 (2)函数计算 (3)打印图形 3.函数的说明文档 (1)函数的说明文档的作用 (2)函数说明文档的语法 (3)查看函数的说明文 ...
- mybatis复杂映射
1. 类型名对应 当实体类与表中字段完全一致时,mapper文件里返回类型用resultType,否则要用resultMap,并且建立resultMap映射 package com.rf.domain ...
- SuperEdge 易学易用系列-SuperEdge 简介
关于 SuperEdge SuperEdge 是由腾讯.Intel.VMware.虎牙直播.寒武纪.首都在线和美团等多家公司共同发起的边缘容器管理系统,它基于原生 Kubernetes.针对边缘计算和 ...
- IDEA工具-快捷键整理
在使用IDEA编辑器的过程中如果能够熟练的使用快捷键将大大的提高工作的效率,以下列为IDEA编辑器使用频率比较高的快捷键 Ctrl+E:显示最近编辑的文件列表 Ctrl+P:显示方法的参数信息 Ctr ...
- python -- 面向对象编程(属性、方法)
一.属性 对象的属性(attribute)也叫做数据成员(data member). 如果想指向某个对象的属性,可以使用格式: object.attribute 属性又分为:私有属性和公有属性. 私有 ...