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. 编译x86_64 Linux内核并基于QEMU运行

    编译并运行内核镜像 安装包准备 $ sudo apt install git $ sudo apt install build-essential kernel-package fakeroot li ...

  2. CentOS-关闭防火墙和禁用安全策略

    关闭防火墙 默认使用的是firewall作为防火墙 查看防火墙状态 $ firewall-cmd --state 停止firewall $ systemctl stop firewalld.servi ...

  3. 资源:jenkins下载路径

    Jenkins版本下载路径 所有版本:http://mirrors.jenkins.io/war/

  4. leetcode TOP100 字母异位词分组

    字母异位词分组 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 思路: 一个map,将每个字符串字符进行记数,字符作为map的key,次数初始为零,以此来标识字 ...

  5. 前端-HTML基础+CSS基础

    .pg-header { height: 48px; text-align: center; line-height: 48px; background-color: rgba(127, 255, 2 ...

  6. XP共享打印机

    1.开启GUEST:右击"我的电脑"管理--用户--GUEST开启 2.运行--GPEDIT.MSC--计算机管理-WINDOWS设置--安全设置--本地策略--用户权利指派--允 ...

  7. mysql 远程登录 10038提示,无法连接

    mysql 默认使用端口3306 MYSQL服务器:WIN7 控制面板,WINDOWS防火墙--高级设置--入站规则--新建规则--端口--TCP-特定本地端口--3306--允许连接--下一步,至完 ...

  8. MapReduce学习总结之java版wordcount实现

    一.代码实现: package rdb.com.hadoop01.mapreduce; import java.io.IOException; import org.apache.hadoop.con ...

  9. Unittest方法 -- 测试分离

    一.下面是it.py 脚本,把浏览器前置和后置条件分离了"""套件公用测试类可进行分离"""import unittestfrom sele ...

  10. ODOO14笔记---系统升级崩溃后进不去系统解决办法

    一.通过pycharm升级模块:  2.对于已安装odoo模块,升级报错系统崩溃的解决办法:---执行以下SQL     update ir_module_module set state ='ins ...