class 调用的独特方法是什么?
What are the unique methods of a class called?
假设您有一个 class,并且您将其定义为:
class A:
pass
当你使用函数时:
dir(A)
returnsclass包含的所有方法和属性:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
我的问题是,__class__
、__delattr__
、__dict__
、__init__
等前后带有双下划线的方法叫什么?我假设它们被称为构造函数,但经过研究 - 似乎该术语仅指 __init__
函数。有谁知道是否有特定的类别来调用这些方法?
这些由所谓的special methods and special attributes组成。根据语言规范,这是这些方法和属性的正式名称。
在 the Python glossary 中,您可以看到他们的条目:
special method
A method that is called implicitly by Python to execute a certain operation on a type, such as addition. Such methods have names starting and ending with double underscores. Special methods are documented in Special method names.
魔术和 dunder(方法)是社区中另外两个常用的术语。
作为旁注,值得一提的是 dir
没有 报告所有这些。
他们被称为special method names:
Special method names
A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.
Some of the type descriptions below contain a paragraph listing ‘special attributes.’ These are attributes that provide access to the implementation and are not intended for general use.
以双下划线开头并以双下划线结尾的名称是保留名称。见 Reserved classes of identifiers:
__*__
System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. Any use of __*__
names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.
它们有时被称为双下划线名称,作为双下划线的缩写。 魔术名称也是他们的流行术语,但都不是官方的。
注意__init__
方法是实例initialiser hook,它不是构造函数。调用此方法时已经构造了实例。如果你需要挂钩到构造中,实现 __new__
method.
假设您有一个 class,并且您将其定义为:
class A:
pass
当你使用函数时:
dir(A)
returnsclass包含的所有方法和属性:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
我的问题是,__class__
、__delattr__
、__dict__
、__init__
等前后带有双下划线的方法叫什么?我假设它们被称为构造函数,但经过研究 - 似乎该术语仅指 __init__
函数。有谁知道是否有特定的类别来调用这些方法?
这些由所谓的special methods and special attributes组成。根据语言规范,这是这些方法和属性的正式名称。
在 the Python glossary 中,您可以看到他们的条目:
special method
A method that is called implicitly by Python to execute a certain operation on a type, such as addition. Such methods have names starting and ending with double underscores. Special methods are documented in Special method names.
魔术和 dunder(方法)是社区中另外两个常用的术语。
作为旁注,值得一提的是 dir
没有 报告所有这些。
他们被称为special method names:
Special method names
A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.
Some of the type descriptions below contain a paragraph listing ‘special attributes.’ These are attributes that provide access to the implementation and are not intended for general use.
以双下划线开头并以双下划线结尾的名称是保留名称。见 Reserved classes of identifiers:
__*__
System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. Any use of__*__
names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.
它们有时被称为双下划线名称,作为双下划线的缩写。 魔术名称也是他们的流行术语,但都不是官方的。
注意__init__
方法是实例initialiser hook,它不是构造函数。调用此方法时已经构造了实例。如果你需要挂钩到构造中,实现 __new__
method.