【发布时间】:2019-06-14 21:19:18
【问题描述】:
这将是一个非常n00b的问题......
在 python 中,我想编写一些函数来检索几种不同类型设备的软件版本和系统正常运行时间。我可以为每个需求编写一个函数,但我不想那样做。我想在函数中编写函数,所以我可以用点号来调用它们。
以下是我希望创建的函数示例:
get(device) # Determine device type and get software version and uptime
get.version(device) # Determine device type and get software version
get.uptime(device) # Determine device type and get device up time
get.cisco.version(device) # Get software version for a Cisco device
get.cisco.uptime(device) # Get device up time for a Cisco device
get.arris.version(device) # Get software version for an Arris device
get.arris.uptime(device) # Get device up time for an Arris device
get.motorola.version(device) # Get software versino for a Motorola device
get.motorola.uptime(device) # Get device up time for a Motorola device
所以,我会编写一个“get”函数,并在其中编写一个“version”、“uptime”、“cisco”、“arris”和“motorola”函数。在这些函数中,我会编写更多函数。
这在 Python 中叫什么?它是如何实施的?我只需要知道我在寻找什么,这样我就可以在文档中找到它并进行学习。
【问题讨论】:
-
它们被称为“嵌套”函数,有时也被称为“本地”函数。
-
你实际上不能在python中嵌套函数,不正确。如果您在 python 控制台上尝试它,您会发现尝试从外部函数外部访问嵌套函数会产生 AttributeError。
-
x/y 问题:似乎您可以使用属于该类的类和函数来做到这一点。在这种情况下,可以查看
abc.AbstractBaseClass并有几个类(cisco、arris 等)从您的抽象实现继承。
标签: python