The differences of _single_starting_underscore, __double_starting_underscores, __double_underscores__

本文详细介绍了Python中定义方法或参数的不同方式,包括单下划线开头的方法仅能被类本身及其子类调用;双下划线开头的方法在类外部无法直接访问,也无法在子类中覆盖;双下划线开头和结尾的方法为特殊方法,由Python内部使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Define a method or parameter with an underscore as the starting

If a method starts with an underscore, it only can be called by this class itself or subclass. Of course, it can not be imported by using "from module import *".

 

2. Define a method or parameter with double underscores as the starting not an ending

It looks like a private method or parameter. It can't be called outside the class, including the subclass. Of course, it can't be overridden by subclass.

 

3. Define a method or parameter with double underscores as the starting and ending

FYI: http://docs.python.org/reference/datamodel.html#specialnames

Summary:
Named special method name.

You are not supposed to call these methods or parameters directly although they are not declared as private. Because they are called by python. Such as the method __getitem__(), __init__()   ---called when the instance is created, __getattr__(), etc...

 

 

Besides, you are not suggested to define a common method or parameter with the underscores on the starting or ending position.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值