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. 【PC桌面软件的末日,手机移动端App称王】写在windows11支持安卓,macOS支持ios,龙芯支持x86和arm指令翻译

    面对这场突如其来的变革,作为软件开发者,应该如何选择自己今后的发展方向?桌面软件开发领域还有前景吗? 起源 自从苹果发布m1处理器,让自家Mac支持IOS移动端app运行之后,彻底打破了移动端app和 ...

  2. Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)

    MyBatis-Plus是MyBatis的增强工具,Generator通过MyBatis-Plus快速生成Entity.Mapper.Mapper XML.Service.Controller等模块的 ...

  3. 【网络编程】HTTP简介&URL

    目录 前言 1. http 简介 1.1 概念 1.2 原理 1.3 特点 2. URL 简介 2.1 概念 2.2 URL 通用格式 2.3 网页地址 实例说明 3. HTTP 消息结构 3.1 客 ...

  4. Laravel使用Observer(观察者)

      1.创建observer文件,我这里是要记录仓库库存模块的操作日志,所以执行下面的语句,会在app/Observers下面创建WarehouseInventoryObserver文件. php a ...

  5. 第12次抽考(GUI)

    1. package week4; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextFiel ...

  6. scrapy 配置文件的详细描述

    # 项目名称 BOT_NAME = 'anjvke' # 爬虫文件所在目录 SPIDER_MODULES = ['anjvke.spiders'] # 创建爬虫文件的模板,创建好的爬虫文件会放在此目录 ...

  7. java基础---java8 新特性

    1. 函数式接口 函数式接口主要指只包含一个抽象方法的接口,如:java.lang.Runnable(java1.0).java.util.Comparator接口(java1.4)等. Java8提 ...

  8. C语言:缓冲区

    缓冲区(Buffer)又称为缓存(Cache),是内存空间的一部分.也就是说,计算机在内存中预留了一定的存储空间,用来暂时保存输入或输出的数据,这部分预留的空间就叫做缓冲区(缓存).有时候,从键盘输入 ...

  9. C语言警告提示

    [Warning] incompatible implicit declaration of built-in function 'strlen' [enabled by default] 提示:st ...

  10. C编译器

    GCC:GNU Compiler Collection,即 GNU 编译器套件.TDM-GCC 4.8.1 32-BIT Release: 查看GCC的版本:C:\Program Files\Dev- ...