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的更多相关文章

  1. Python: import vs from (module) import function(class) 的理解

    Python: Import vs From (module) import function(class) 本文涉及的 Python 基本概念: Module Class import from . ...

  2. ImportError: dynamic module does not define module export function (PyInit__sqlite3)

    使用python3.6 中的django-admin创建项目的时候报错 ImportError: dynamic module does not define module export functi ...

  3. 编译caffe的Python借口,提示:ImportError: dynamic module does not define module export function (PyInit__caffe)

    >>> import caffeTraceback (most recent call last): File "<stdin>", line 1, ...

  4. Erlang Module and Function

    Module   -module(Name). 模块是方法的集合.注意这行最后的“.”符号是必不可少的. 这个模块名必须和保存这段代码的文件(后缀为“erl”的文件)有相同的名称. 当我们在使用另一个 ...

  5. ImportError: dynamic module does not define module export function (PyInit__caffe)

    使用python3运行caffe,报了该错误. 参考网址:https://stackoverflow.com/questions/34295136/importerror-dynamic-module ...

  6. 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 ...

  7. function module 之间调用

    1: 在一个function group 中定义一个function module 2:在另外一个module中调用该module "调用其它function 要用 单引号 引着. 一个mo ...

  8. pytest fixture中scope试验,包含function、module、class、session、package

    上图是试验的目录结构 conftest.py:存放pytest fixture的文件 import uuid import pytest @pytest.fixture(scope="mod ...

  9. nodejs模块中exports和module.exports的区别

    通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ ,这些核心模块被编译成二进制文件,可以require('模块名')去获取:核心 ...

随机推荐

  1. 并发王者课-铂金9:互通有无-Exchanger如何完成线程间的数据交换

    欢迎来到<并发王者课>,本文是该系列文章中的第22篇,铂金中的第9篇. 在前面的文章中,我们已经介绍了ReentrantLock,CountDownLatch,CyclicBarrier, ...

  2. 重新整理 .net core 实践篇————跨域问题四十一]

    前言 简单整理一下.net core 的跨域问题,这个以前也整理过比较详细的,故而在此简单整理一下. 正文 对跨域相对的就是同源,什么是同源呢? 协议相同(http/https) 主机(域名)相同 端 ...

  3. 创建Maven项目骨架并使用

    1.archetype是什么? archetype 字面意思是 原型.可以理解为archetype相当于一个脚手架/模板,通过这个脚手架/模板我们可以快速的创建出一个项目. 比如下图中的这些就是mav ...

  4. linux菜鸡学习之路

    Linux入门 Linux 介绍 1.Linux怎么读 2.Linux是一款操作系统,免费,开源,安全,高效,稳定,处理高并发非常强悍. Linux文件系统目录 基本介绍 linux的文件系统树状目录 ...

  5. vuejs知识总结

    1.Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的.ViewModel是Vue.js的核心,它是一个Vue实例. <!DOCTYPE html& ...

  6. QT从入门到入土(二)——对象模型(对象树)和窗口坐标体系

    摘要 我们使用的标准 C++,其设计的对象模型虽然已经提供了非常高效的 RTTI 支持,但是在某些方面还是不够灵活.比如在 GUI 编程方面,既需要高效的运行效率也需要强大的灵活性,诸如删除某窗口时可 ...

  7. vue3 script setup 定稿

    vue script setup 已经官宣定稿.本文主要翻译了来自 0040-script-setup 的内容. 摘要 在单文件组件(SFC)中引入一个新的 <script> 类型 set ...

  8. python 15篇 面向对象

    1.面向对象编程概念 面向对象是包含面向过程 面向过程编程 买车: 1.4s看车,买车 2.上保险 保险公司 3.交税 地税局 4.交管所 上牌面向对象编程 卖车处: 1.4s 2.保险 3.交税 4 ...

  9. 成功解决1406, “Data too long for column ‘txt‘ at row 1“

    这是因为数据库里该字段的数据类型所给的数据空间太小.MySQL将截断超过指定列宽度的任何插入值.为了让这个不报错,可以尝试切换MySQL模式不使用严格模式. SET @@global.sql_mode ...

  10. Eclipse配置反编译插件jadclipse

    参考自:https://blog.csdn.net/moneyshi/article/details/79715891 Jad是一个Java的一个反编译工具,是用命令行执行,和通常JDK自带的java ...