如果在Python中的约定是大写类,为什么是list()没有大写?它不是一个类吗? [英] If the convention in Python is to capitalize classes, why then is list() not capitalized? Is it not a class?

查看:628
本文介绍了如果在Python中的约定是大写类,为什么是list()没有大写?它不是一个类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常当我看到类定义 class Foo:时,我总是看到它们以大写字母开头。

Often when I see class definitions class Foo:, I always see them start with upper case letters.

但是,不是列表 [] 或者dict {} 或一些其他内置类型,还有类吗?对于这个问题,输入到Python的IDLE中的一切都是一个类,它是一个自动用紫色标记的关键字(使用Window的二进制分布)。

However, isn't a list [] or a dict {} or some other built-in type, a class as well? For that matter, everything typed into the Python's IDLE which is a keyword that is automatically color coded in purple (with the Window's binary distribution), is itself a class right?

例如 spam = list()

spam 现在一个 list()的实例

所以我的问题是,为什么python允许我们首先做一些像$ list = list(),当没有人,大概,这样做。但是,为什么不是 list = List()

So my question is, why does python allow us to first of all do something like list = list() when nobody, probably, does that. But also, why is it not list = List()

语言的开发人员决定不使用

Did the developers of the language decide not to use any sort of convention, while it is the case that most Python programmers do name their classes as such?

推荐答案

是的,大写字母可以是任何类型的约定,而大多数Python程序员都是这样命名他们的类的?初始类是惯例,如 PEP 8 中所述。

Yes, uppercase-initial classes are the convention, as outlined in PEP 8.

你是正确的,许多内置类型不遵循这个约定。这些是来自Python早期阶段的保存,当用户定义的类和内置类型之间有更大的差别。但是,仍然似乎用C语言编写的内置或扩展类型更可能具有小写名称(例如 numpy.array ,而不是 numpy.Array

You are correct that many builtin types do not follow this convention. These are holdovers from earlier stages of Python when there was a much bigger difference between user-defined classes and builtin types. However, it still seems that builtin or extension types written in C are more likely to have lowercase names (e.g., numpy.array, not numpy.Array).

但是,约定是在Python代码中为你自己的类使用大写初始。

Nonetheless, the convention is to use uppercase-initial for your own classes in Python code.

这篇关于如果在Python中的约定是大写类,为什么是list()没有大写?它不是一个类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆