3

I just finished a small all-python3 gpio module for use with Linux gpiolib. I've toyed with two different ways of naming and importing things in the client:

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)

Question: Is there a good/concrete reason to prefer one over the other? Or is it just preference?

There isn't (for me) really a happy hybrid. I like the short names when scoped inside of the module, but if used outside of the module, really need the module name to qualify what they are. But importing gpio, and then referring to gpio.GPIOOutput seems redundant.

Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
  • I really think this depends on the use case. If you are going to have people doing things like `from gpio import *` then yeah go with the `GPIO` prefix scheme. – mustachioed Apr 25 '18 at 19:48
  • 1
    I think `import *` is something that's never encouraged except in the `repl`. – Travis Griggs Apr 25 '18 at 19:49
  • 1
    It is up to personal preference. If I am only using one class in a module then I prefer to use from module import class. If I am using multiple classes then I simply use import module. – Joshua Yonathan Apr 25 '18 at 19:52

1 Answers1

5

PEP8 argues for gpio.Input(), etc.

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.

Community
  • 1
  • 1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308