python类方法是什么_python – 一个叫做类的独特方法是什么?

如果您有一个类,并将其定义为:

class A:

pass

当你使用这个功能时:

dir(A)

返回该类包含的所有方法和属性:

['__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 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.

__*__

System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the 07003 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.

它们有时被称为dunder名称,作为双下划线的缩写.魔术名称也是他们的流行术语,但两者都不是官方名称.

请注意,__ init__方法是实例初始化器钩子,它不是构造函数.该方法在调用此方法时已经构建.如果您需要挂钩施工,请执行__new__ method.

标签:python,oop,python-3-x,naming

来源: https://codeday.me/bug/20190611/1216606.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值