Python编程进阶:从模块使用到类的构建
1. 模块导入与对象重命名
在Python中,当导入对象时,若不喜欢对象的名称或觉得名称过长,可以使用 from import as 语句对其进行重命名。例如:
>>> from push import push_commands as pc
>>> pc(device, commands)
Connecting to device: router1
Sending command: interface Eth1/1
Sending command: shutdown
通过这种方式,能让对象名称更简短、直观。
2. 函数文档化
为了让函数更易于复用,需要对函数进行文档化。在Python里,可以使用三引号 """ 来定义文档字符串(docstring),以此描述函数、方法和类,同时记录预期的参数和函数的返回值。示例如下:
def get_commands(vlan, name):
"""Get commands to configure a VLAN.
Args:
vlan (int): vlan id
name (str): name of the vlan
Returns:
List of commands is returned.
"""
comma
Python进阶:模块与类实战
超级会员免费看
订阅专栏 解锁全文
962

被折叠的 条评论
为什么被折叠?



