某项目中,我们的代码使用的2个不同库中的图形类:

  Circle,Triangle

这两个类中都有一个获取面积的方法接口,但是接口的名字不一样

统一这些接口,不关心具体的接口,只要我调用统一的接口,对应的面积就会计算出来

如何解决这个问题?

  定义一个统一的接口函数,通过反射:getattr进行接口调用

#!/usr/bin/python3

from math import pi

class Circle(object):
def __init__(self, radius):
self.radius = radius def getArea(self):
return round(pow(self.radius, 2) * pi, 2) class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height def get_area(self):
return self.width * self.height # 定义统一接口
def func_area(obj):
# 获取接口的字符串
for get_func in ['get_area', 'getArea']:
# 通过反射进行取方法
func = getattr(obj, get_func, None)
if func:
return func() if __name__ == '__main__':
c1 = Circle(5.0)
r1 = Rectangle(4.0, 5.0) # 通过map高阶函数,返回一个可迭代对象
erea = map(func_area, [c1, r1])
print(list(erea))

  

  通过标准库operator中methodcaller方法进行调用

#!/usr/bin/python3

from math import pi
from operator import methodcaller class Circle(object):
def __init__(self, radius):
self.radius = radius def getArea(self):
return round(pow(self.radius, 2) * pi, 2) class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height def get_area(self):
return self.width * self.height if __name__ == '__main__':
c1 = Circle(5.0)
r1 = Rectangle(4.0, 5.0) # 第一个参数是函数字符串名字,后面是函数要求传入的参数,执行括号中传入对象
erea_c1 = methodcaller('getArea')(c1)
erea_r1 = methodcaller('get_area')(r1)
print(erea_c1, erea_r1)

  

python_如何通过实例方法名字调用方法?的更多相关文章

  1. Delphi按名字调用方法高级解决方案

    转帖于https://lfzhs.iteye.com/blog/980200 按名字调用方法似乎一直以来都是大家比较关注的技术,在论坛上有一个经典的答复: type TProcedure = proc ...

  2. Java中的静态方法和实例方法的调用的理解(不同的类下的方法调用)

    public class MethodCall { public static void main(String[] args) { Test.sayStatic(); Test test = new ...

  3. Python的程序结构[1] -> 方法/Method[0] -> 类实例方法、私有方法和抽象方法

    类实例方法.私有方法和抽象方法 Python中最常用的就是类实例方法,类似于属性中的类实例属性,同时,也存在与私有属性类似方法,即私有方法,下面介绍这两种常见的方法,以及一种特殊意义的类实例方法 -- ...

  4. phpcms 的实用相关接口,函数,调用方法

    常用函数 , 打开include/global.func.php,下面存放一些公共函数view plaincopy to clipboardprint? strip_tags() 调用内容过滤html ...

  5. C# 程序开始主要是写类和方法 的基本步骤和调用方法

    主程序的使用方式以及调用方法字段.属性.方法 using System; using System.Collections.Generic; using System.Linq; using Syst ...

  6. js 函数闭包内部返回函数体调用方法难点解答

    今天在网上,看到一篇关于js函数难点的文章,js函数的一些难点.在那上面提了一下,关于js函数返回另一个函数的问题,并附上了一道面试题: var add = function(x){ var sum ...

  7. phpcms常用接口调用方法

    常用函数 , 打开include/global.func.php,下面存放一些公共函数 view plaincopy to clipboardprint?function str_charset($i ...

  8. NDK开发之调用方法

    与NDK开发之访问域中介绍的一样,Java中的方法也是分为两类:实例方法和静态方法.JNI提供了访问两类方法的函数,下面我们一起来看看怎么在C中访问Java中的方法. 我们的MainActivity中 ...

  9. phpcms v9联动菜单的调用方法_详解get_linkage函数

    phpcms v9联动菜单调用方法[此为内容页调用方法]: {get_linkage($areaid,1,' >> ',1)} 显示效果: phpcms吧 >> 模板下载 &g ...

随机推荐

  1. 20 Zabbix系统性能优化建议

    点击返回:自学Zabbix之路 20 Zabbix系统性能优化建议 1. Zabbix性能变慢的可能表现: zabbix队列有太多被延迟的item,可以通过administration-queue查看 ...

  2. Java学习笔记8---类的静态成员变量与静态成员方法的访问与调用方式

    (注:静态变量修改为静态成员变量,静态方法改为静态成员方法) 静态成员变量又称类变量,静态成员方法又称类方法,它们统称为静态成员或类成员.静态成员由static修饰,是属于整个类的,所有的对象共享这些 ...

  3. vue+Element实现静态旅游网站

    页面效果: 1.用vue脚手架:vue-cli,新建一个vue项目. 2.npm run dev后,给小颖了一句提示:Your application is running here:http://l ...

  4. 【转】NO.3、python+appium+ios,遍历真机元素,得到webview

    pyhton+appium+iOS,遍历真机webview.是遍历真机的webview,遍历模拟器的webview请另寻方法. 1.mac上安装ios_webkit_debug_proxy 命令:br ...

  5. ionic2 安装(一)

    1.安装java JDK 2.安装nodejs 3.安装最新版ionic 指令:npm install ionic@latest 4.安装cordova 指令:npm install -g cordo ...

  6. opencv提供的带参数例程

    body { font-family: @微软雅黑; font-size: 8pt; line-height: 1.5 } html,body { color: inherit; background ...

  7. 解决api、WebService跨域问题

    webapi接口在ajax调用的很多情况下都会出现跨域问题,同样的WebService如果想用ajax调用,也需要接口跨域问题,解决方案如下: 1.IIS配置 打开IIS选择发布后的webapi或者是 ...

  8. DAY4-打卡第四天-2018-1-12

    刚经历C语言考试,提前一个小时交卷出来在学一点咯!! 字符串不是一个基本类型,不能用恒等== 而应该用: 变量名.equals(""); 变量名.equalsIgnoreCase( ...

  9. 单张滑动tab 组件

    /* CSS重置 * */ body, ul, ol { margin: 0px; padding: 0px; } .flash { width: 300px; height: 420px; posi ...

  10. BZOJ 3668: [Noi2014]起床困难综合症【贪心】

    3668: [Noi2014]起床困难综合症 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2326  Solved: 1305[Submit][St ...