English 中文(简体)
界面和抽象分类的 Python 命名惯例?
原标题:Python naming conventions for interfaces and abstract classes?

Python 中命名界面和抽象类的公约是什么? < a href="https://www.python.org/dev/peps/pep-0008/" rel= “norefererr” >PEP 8 不讨论这个问题。例如,在爪哇, 界面通常用 I 前缀命名。 在 PHP (主要命名空间)中, 抽象类通常用 后缀命名。

(我知道Pythonists并不热衷于界面,也许这就是为什么我无法在这个问题上找到很多共识的原因。 但使用界面和抽象类并不是我的问题。 )

最佳回答

我认为命名一个与 >I 前缀的接口是完全可以接受的。

例如:

  • IFoo
  • IPublishable

几年前我使用Zope接口,我注意到大多数代码库都使用这个会议,我们的团队也使用这个会议。

我们更喜欢 IFoo vs. Foo Interface IFoo Infoo Interface

问题回答

TL;DR

如果您不关心"https://www.python.org/dev/peps/pep-0484/"rel="noreferr" 类型提示 ,请随你命名。否则,"https://docs.python.org/3/library/abc.html" rel="noreferr"\\\code >ABC > 就是其他开发商的API。他们的名字应该干净,没有杂物。具体实施应该告诉你为什么它是特别的。

调用您的抽象类 用户 和安装类 Server用户 Liberuser用户


Why would we need such a convention in the first place?

首先应该问他们自己, 我们为什么要关心班级命名呢?

(它不是python),您的接口的名称代表一个API,其他开发者通过它与您的代码互动。因此,函数的调用者应迅速理解参数和返回类型的含义。例如,Java (

public void signupUsers(Collection<User> users) {
    // TODO: do something with users
}

我们希望名称尽可能纯洁。 我们接受任何 collection 和任何 user 。 它可能是 ArrayList , HashSet , LoblicLiveerUser ServerUser , 我们不在乎。

Why is there no such standard in Python?

这在python中并不相关,因为它是一个ABC s (如果他们与界面的作用相似),应该有一个干净的名称。类似 User 的名称比 IUser 要好得多。如果你的用户有具体的实施,该实施的名称应该告诉你它有什么特别之处。它是否是一个用户,用服务器的数据更新数据?它叫它Serveruser 。你是否将用户数据存储在本地?调它Lionuser

def signup_users(users: Iterable[User]) -> None:
    pass  # TODO: do something with users

关注执行细节的“丑陋”代码会是这样的:

lovelace = LocalUser("Aida", "Lovelace", 1815)
babbage = ServerUser("htts://some_service.com/users/charles_babbage")
signup_users([aida, babbage])

这就使必须处理执行问题的打电话者承担处理复杂名字的负担----正如它应该处理的那样





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签