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. 对象池技术和通用实现GenericObjectPool

    对象池技术其实蛮常见的,比如线程池.数据库连接池 他们的特点是:对象创建代价较高.比较消耗资源.比较耗时: 比如 mysql数据库连接建立就要先建立 tcp三次握手.发送用户名/密码.进行身份校验.权 ...

  2. Nginx:Nginx的安装

    Nginx安装 首先安装依赖 #安装Nginx需要gcc openssl-devel pcre-devel zlib-devel依赖 yum -y install gcc openssl-devel ...

  3. 0shell变量

    1.定义变量 2.使用变量 3.修改变量的值 4.将命令的结果赋值给变量 5.只读变量 6.删除变量 一.变量 1.定义变量 在 Bash shell 中,每一个变量的值都是字符串,无论你给变量赋值时 ...

  4. Whitzard OJ Introduce to packing

    1.概述 这个就是个smc,为什么会归于加壳,我个人理解是和UPX的运行方式有点像把,不对应该是说和压缩壳的运行方式 很相似,都是先运行一段解密代码,之前的符号表也替换了下 2.解题 有两种方式一种是 ...

  5. 跨域解决之JSONP和CORS的详细介绍

    JSONP跨域和CORS跨域 什么是跨域? 跨域:指的是浏览器不能执行其它网站的脚本,它是由浏览器的同源策略造成的,是浏览器的安全限制! 同源策略 同源策略:域名.协议.端口均相同. 浏览器执行Jav ...

  6. leetcode TOP100 两数相加

    两数相加 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  7. Android Binder 进程间通讯机制梳理

    什么是 Binder ? Binder是Android系统中进程间通讯(IPC)的一种方式,也是Android系统中最重要的特性之一.Binder的设计采用了面向对象的思想,在Binder通信模型的四 ...

  8. final修饰符(6)-final类

    final修饰的类不可以有子类,例如:java.lang.Math类就是一个final类,它不可以有子类 子类继承父类,可以访问父类的内部数据,可以重写父类的方法来改变父类方法的实现细节,可能导致不安 ...

  9. 最大网络流dinic

    初始化flow(最大流量)为INF(极大值),建边(正向弧和反向弧) bfs寻找增广路看看有没有路,顺便进行深度标号.如果没有路直接结束输出flow. 如果有,我们按照深度dfs.dfs时注意在给正向 ...

  10. 【Uva11400 Lighting System Design】动态规划

    分析 先按照电压从小到大排序,做一下前缀和s[i]求i之前的电灯泡的数量. 状态:$ F_i\(表示到\) i$个灯泡的最小开销. 状态转移方程:$ F_i=F_j+(s[i]-s[j])\times ...