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('模块名')去获取:核心 ...
随机推荐
- Leetcode No.66 Plus One(c++实现)
1. 题目 1.1 英文题目 Given a non-empty array of decimal digits representing a non-negative integer, increm ...
- linux驱动之获取设备树信息
上一篇文章学习了字符设备的注册,操作过的小伙伴都知道上一篇文章中测试驱动时是通过手动创建设备节点的,现在开始学习怎么自动挂载设备节点和设备树信息的获取,这篇文章中的源码将会是我以后编写字符驱动的模板. ...
- 求数组的子数组之和的最大值III(循环数组)
新的要求:一维数组改成循环数组,只是涉及简单算法,只是拿了小数做测试 想法:从文件读取数组,然后新建数组,将文件读取的数组在新数组中做一下连接,成为二倍长度的数组,然后再遍历,将每次遍历的子数组的和存 ...
- Java基础00-分支语句6
1. 流程控制 1.1 流程控制语句概述 1.2 流程控制语句分类 1.3 顺序结构 2. if语句 2.1 if语句格式1 适合一种情况的判断 执行流程图: 2.2 if语句格式2 适合两种情况的判 ...
- [刘阳Java]_美团点评2018届校招面试总结_Java后台开发【转载】
美团喜欢一口气把三轮技术面和HR面一起面完,虽然身心比较累(每一面差不多一个小时),不过也算是一个好事,不像某些公司一天就一面然后让回去等消息,等面试通知也等得让人很焦虑,而且还容易出现面试时间冲突. ...
- Redux-基本概念
相关文档 1) 英文文档: https://redux.js.org/ 2) 中文文档: http://www.redux.org.cn/ 3) Git ...
- 前端开发入门到进阶第一集【使用sublime快速编写Html和Css】
1,安装sublime编辑器,下载地址:http://www.sublimetext.com/3 2,要使用sublime的插件机制必须安装package control:https://packag ...
- sshd_config详解
# $OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 djm Exp $ # This is the sshd server system-wide ...
- Appium - adb monkey事件(二)
操作事件简介 Monkey所执行的随机事件流中包含11大事件,分别是触摸事件.手势事件.二指缩放事件.轨迹事件.屏幕旋转事件.基本导航事件.主要导航事件.系统按键事件.启动Activity事件.键盘事 ...
- java对接c++发布的webservice接口,其中参数类型有base64Binary格式(无需将图片数据转化为c++中的结构体)
接口名称: std::string SendVehiclePass(std::string VehiclePassInfo, struct xsd__base64Binary PlatePicData ...