“class ClassName : pass” 在Python中是什么意思?

huangapple go评论95阅读模式
英文:

What is "class ClassName : pass" in Python?

问题

这个教程中,一个完整的类被定义为class DontAppend: pass。根据这个Stack Overflow的问题和答案pass定义了这个类。

然而,根据谷歌的共识,pass是一个语句。我从未见过语句定义一个类,除非是在类方法的定义中。

作为一个语句,pass也不定义一个具名的类属性/属性。

因为pass并不用于定义方法或属性,那么它究竟如何定义这个类呢?这只是一种特殊的约定,仅用于定义一个空类吗?

英文:

In this tutorial, an entire class is defined as class DontAppend: pass. According to this Q&A on Stack Overflow, pass defines the class.

The consensus from Google, however, is that pass is a statement. I've never seen statements define a class other than within definitions of class methods.

As a statement, pass also doesn't define a named class attribute/property.

Since pass doesn't serve to define a method or an attribute, how does it actually define the class? Is this just a special convention, solely for defining an empty class?

答案1

得分: 2

在Python中,pass语句可以用作未来代码的占位符(或者它可能始终保持为pass)。

pass语句有助于避免不允许空代码的错误。以下都是有效的Python语句 -

class XYX:
    pass

class SomeException(Exception):
    pass

if somecondition:
    pass
else:
    x = y + z

在循环、函数定义、类定义或条件语句中不允许使用空代码。pass在这些情况下很有帮助。通过继承基本异常类并将类主体保留为pass,通常可以创建自定义异常。

英文:

The pass statement in python can be used as a placeholder for future code (or it may always remain as pass).

A pass statement helps in avoiding the error where empty code is not allowed. All the following are valid python statements -

class XYX:
    pass

class SomeException(Exception):
    pass

if somecondition:
    pass
else:
    x = y + z

Empty code is not allowed in loops, function definitions, class definitions, or in if statements. pass helps in such cases. It is pretty common to create custom exceptions by inheriting base exception classes and leaving the class body as pass

答案2

得分: 2

你说

> 根据Stack Overflow上的这个问答pass定义了这个类。

但不,你误读了链接页面。 pass 并没有定义这个类。 class DontAppend: pass 定义了这个类。

在这个用法中,pass 只是类的主体,而不是整个类定义。类主体是一系列任意语句,将被执行以填充类的命名空间。您不希望在此类的主体中做任何事情,因此pass,一个不执行任何操作的语句,是一个适当的主体使用。

在类主体执行之后(并且不执行任何操作,因为它只是pass),class 语句将获取该命名空间(大部分为空,除了一些默认内容)并基于该命名空间创建一个新的类型对象。

英文:

You say that

> According to this Q&A on Stack Overflow, pass defines the class.

but no, you've misread the linked page. pass doesn't define the class. class DontAppend: pass defines the class.

In this usage, pass is just the class body, not the entire class definition. The class body is a sequence of arbitrary statements that will be executed to populate the class's namespace. You don't want to do anything in the body of this class, so pass, a statement that doesn't do anything, is an appropriate body to use.

After the class body executes (and does nothing, because it's just pass), the class statement will take the namespace (mostly empty, except for a few default contents) and create a new type object based on that namespace.

答案3

得分: 1

以下是有关Python类定义语句的语法规范的说明:

classdef    ::=  [decorators] "class" classname [inheritance] ":" suite
inheritance ::=  "(" [argument_list] ")"
classname   ::=  identifier

因此,如您所见,冒号 : 后的所有内容都属于一个 "suite"(通常,这个 suite 被称为 "类体" 或 "类定义语句的主体")。

以下是一个 suite 的规范

suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt ";" simple_stmt* ";"

因此,基本上,"suite" 就是一系列语句。以下是一个有效的类定义示例:

class Foo:
    print("类体内部")

(请注意,类体是 可执行的,类定义是可执行代码)。因此,任何有效的 Python 语句都可以放在类体中。那么 pass 是什么呢?这是规范

pass_stmt ::=  "pass"

pass 是一个空操作 — 当执行它时,什么都不会发生。当在语法上需要语句但不需要执行代码时,它非常有用,例如:

def f(arg): pass    # 一个什么都不做的函数(暂时)
class C: pass       # 没有方法的类(暂时)

因此,在类体中需要某些语句,如果您希望类为空,您可以简单地使用 pass

请注意,如果您想要了解有关类定义语句如何执行以及该命名空间如何成为类对象的命名空间的更详细信息,此数据模型链接将很有用

英文:

Here is the specification for the syntax of the python class definition statement:

classdef    ::=  [decorators] "class" classname [inheritance] ":" suite
inheritance ::=  "(" [argument_list] ")"
classname   ::=  identifier

So, as you can see, everything after the colon : is part of a "suite" (generally, this suite is called "the class body", or the "body of the class definition statement").

Here is the specification for a suite:

suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]

So basically, a "suite" is any series of statements. Here is a valid class definition:

>>> class Foo:
...     print("inside the class body")
...
inside the class body
>>>

(Note, the body is executed, a class definition is executable code). So any valid Python statement can go in a class body. So what is pass then? Here is the spec:

> pass_stmt ::= "pass"
>
> pass is a null operation — when it is executed, nothing happens. It is
> useful as a placeholder when a statement is required syntactically,
> but no code needs to be executed, for example:
>
> def f(arg): pass # a function that does nothing (yet)
>
> class C: pass # a class with no methods (yet)

So some statement is required in the class body, you cannot just leave it blank if you want the class to be empty. So in that case, you can simply use pass.

Note, if you want more nitty-gritty details about how a class definition statement is executed and how that namespace becomes the namespace of the class object, this link to the data model will be useful

答案4

得分: 0

在Python中,类是对象,它们可以用来表示值或概念。在这种情况下,DontAppend是一个空类,没有定义任何方法或属性。它仅仅被用作一个唯一的对象,用作good_function中new_elem参数的默认值。

通过将new_elem默认为DontAppend,函数可以检查是否已经传递了一个特定的值给new_elem,方法是将其与DontAppend进行比较。如果提供了不同的值,这意味着调用者明确传递了一个new_elem的值,而这个值将被附加到starter_list中。如果new_elem是DontAppend,表示没有提供特定的值,函数会跳过将其附加到列表中。

实质上,DontAppend充当了一个特殊的标志值,用于区分提供了特定值的情况和没有提供特定值的情况。这使得函数调用的灵活性更高,并提供了处理可选参数的方法。

英文:

In Python, classes are objects, and they can be used to represent values or concepts. In this case, DontAppend is an empty class with no methods or attributes defined. It is simply used as a unique object to serve as a default value for the new_elem argument in good_function.

By defaulting new_elem to DontAppend, the function can check if a specific value has been passed for new_elem by comparing it to DontAppend. If a different value is provided, it means that the caller explicitly passed a value for new_elem, and that value will be appended to the starter_list. If new_elem is DontAppend, it indicates that no specific value was provided, and the function skips appending to the list.

Essentially, DontAppend acts as a special sentinel value to distinguish between the cases where a specific value is provided and where no specific value is provided for new_elem. It allows for more flexibility in how the function is called and provides a way to handle optional arguments.

huangapple
  • 本文由 发表于 2023年6月29日 02:25:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定