python3 模块 import/naming 困境
python3 module import/naming dilemma
我刚刚完成了一个小型全 python3 gpio 模块,用于 Linux gpiolib
。我试过两种不同的方式在客户端中命名和导入东西:
1
gpio.py
class GPIOInput(object):
...
class GPIOEvent(object):
...
class GPIOOutput(object):
...
client.py
from gpio import GPIOEvent, GPIOOutput, GPIOInput
irq = GPIOEvent(1, 14)
2
gpio.py
class Input(object):
...
class Event(object):
...
class Output(object):
...
client.py
import gpio
irq = gpio.Event(1, 14)
问题:有 good/concrete 更喜欢其中一个的理由吗?还是只是偏好?
没有(对我来说)真正快乐的混合体。我喜欢模块内部范围内的短名称,但如果在模块外部使用,真的需要模块名称来限定它们是什么。但是导入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 gpio 模块,用于 Linux gpiolib
。我试过两种不同的方式在客户端中命名和导入东西:
1
gpio.py
class GPIOInput(object):
...
class GPIOEvent(object):
...
class GPIOOutput(object):
...
client.py
from gpio import GPIOEvent, GPIOOutput, GPIOInput
irq = GPIOEvent(1, 14)
2
gpio.py
class Input(object):
...
class Event(object):
...
class Output(object):
...
client.py
import gpio
irq = gpio.Event(1, 14)
问题:有 good/concrete 更喜欢其中一个的理由吗?还是只是偏好?
没有(对我来说)真正快乐的混合体。我喜欢模块内部范围内的短名称,但如果在模块外部使用,真的需要模块名称来限定它们是什么。但是导入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.