python3模块导入/命名困境

标签 python python-3.x naming-conventions python-module

我刚刚完成了一个用于 Linux gpiolib 的小型全 python3 gpio 模块。我试过两种不同的方式在客户端中命名和导入内容:

1

gpio.py

class GPIOInput(object):
    ...

class GPIOEvent(object):
    ...

class GPIOOutput(object):
    ...

客户端.py

from gpio import GPIOEvent, GPIOOutput, GPIOInput

irq = GPIOEvent(1, 14)

2

gpio.py

class Input(object):
    ...

class Event(object):
    ...

class Output(object):
    ...

客户端.py

import gpio

irq = gpio.Event(1, 14)

问题:是否有充分/具体的理由来选择一个而不是另一个?还是只是偏好?

没有(对我而言)真正快乐的混合体。我喜欢模块内部范围内的短名称,但如果在模块外部使用,真的需要模块名称来限定它们是什么。但是导入 gpio,然后引用 gpio.GPIOOutput 似乎是多余的。

最佳答案

PEP8争论 gpio.Input()

There's also the style of using a short unique prefix to group related names together. This is not used much in Python, ...

In Python, this style is generally deemed unnecessary because attribute and method names are prefixed with an object, and function names are prefixed with a module name.

关于python3模块导入/命名困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50030199/

相关文章:

python - 当我尝试打开模块时 pudb3 引发 TypeError

objective-c - 是否有可用于描述人的 Cocoa 初始值设定项?

python - 在 django/python 中组织通用代码

python - 如何检查 .xls 和 .csv 文件是否为空

python - 如何使用 json 将编码音频字符串传递给字节?

naming-conventions - 验证方法的方法名称中的前缀 “Is”

Android:命名资源

python - 在Azure Kubernetes服务中部署时导入cv2错误-python

python - 如何用新数据更新 SVM 模型

python - 将网页中的表格数据写入 csv 文件