【python】python _、__、__xx__之间的差别
本文来自 yzl11 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yzl11/article/details/53792416?utm_source=copy
单下划线、双下划线、头尾双下划线说明:
1. __foo__: 定义的是特殊方法,一般是系统定义名字 ,类似 __init__() 之类的。
当你看到"__this__"的时,就知道不要调用它。为什么?因为它的意思是它是用于Python调用的,如下
>>> name = "igor" >>> name.__len__() 4 >>> len(name) 4 >>> number = 10 >>> number.__add__(20) 30 >>> number + 20 30
“__xx__”经常是操作符或本地函数调用的magic methods。在上面的例子中,提供了一种重写类的操作符的功能。
在特殊的情况下,它只是python调用的hook。例如,__init__()函数是当对象被创建初始化时调用的;__new__()是用来创建实例
class CrazyNumber(object):
def __init__(self, n):
self.n = n
def __add__(self, other):
return self.n - other
def __sub__(self, other):
return self.n + other
def __str__(self):
return str(self.n) num = CrazyNumber(10)
print num #
print num + 5 #
print num - 20 # 30 ---------------------
另一个例子:
class Room(object):
def __init__(self):
self.people = []
def add(self, person):
self.people.append(person)
def __len__(self):
return len(self.people) room = Room()
room.add("Igor")
print len(room) # ---------------------
2._foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行访问,不能用于 from module import *
class BaseForm(StrAndUnicode):
... def _get_errors(self):
"Returns an ErrorDict for the data provided for the form"
if self._errors is None:
self.full_clean()
return self._errors errors = property(_get_errors)
上面的代码片段来自于django源码(django/forms/forms.py)。这里的errors是一个属性,属于API的一部分,但是_get_errors是私有的,是不应该访问的,但可以通过errors来访问该错误结果。
3. __foo: 双下划线的表示的是私有类型(private)的变量, 只能是允许这个类本身进行访问了。
class A(object):
def __method(self):
print "I'm a method in A"
def method(self):
self.__method() a = A() a.method() ---------------------
输出:
I'm a method in A
给A添加子类
class B(A):
def __method(self):
print "I'm a method in B" b = B()
b.method()
-------------------------------
输出:
I'm a method in A
【python】python _、__、__xx__之间的差别的更多相关文章
- Python中_,__,__xx__的区别
_xx 单下划线开头 Python中没有真正的私有属性或方法,可以在你想声明为私有的方法和属性前加上单下划线,以提示该属性和方法不应在外部调用.如果真的调用了也不会出错,但不符合规范. #! /usr ...
- Python中_,__,__xx__方法区别
_xx 单下划线开头 Python中没有真正的私有属性或方法,可以在你想声明为私有的方法和属性前加上单下划线,以提示该属性和方法不应在外部调用.如果真的调用了也不会出错,但不符合规范. 方法就是以单下 ...
- python _、__、__xx__之间的差别
默认情况下,Python中的成员函数和成员变量都是公开的(public),在python中没有类public,private等关键词来修饰成员函数和成员变量.其实,Python并没有真正的私有化支持, ...
- python 教程_【python 基础教程详解】
Lesson 1 准备好学习Python的环境下载的地址是:www.python.org为了大家的方便,我在校内作了copy:http://10.1.204.2/tool/compiler&I ...
- 简学Python第二章__巧学数据结构文件操作
#cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...
- 简学Python第一章__进入PY的世界
#cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...
- python 时间字符串和时间戳之间的转换
https://blog.csdn.net/qq_37193537/article/details/78987949 1.将字符串的时间转换为时间戳 方法: a = " ...
- python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学
首发于:python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=431 本文 ...
- python字符串/列表/元组/字典之间的相互转换(5)
一.字符串str与列表list 1.字符串转列表 字符串转为列表list,可以使用str.split()方法,split方法是在字符串中对指定字符进行切片,并返回一个列表,示例代码如下: # !usr ...
随机推荐
- Java NIO学习笔记八 DatagramChannel
Java NIO DatagramChannel Java NIO DatagramChannel是可以发送和接收UDP数据包的通道.由于UDP是一种无连接网络协议,因此您不能默认读取和写入Datag ...
- 【leetcode_easy】532. K-diff Pairs in an Array
problem 532. K-diff Pairs in an Array 题意:统计有重复无序数组中差值为K的数对个数. solution1: 使用映射关系: 统计数组中每个数字的个数.我们可以建立 ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- iOS-意见反馈UITextView的使用+不能输入字符输入
@interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>@property (nonat ...
- python基础----redis模块
数据库 关系型数据 例如mysql,有表还有约束条件等 非关系型 k-v形式 memcache 存在内存中 redis 存在内存 mongodb 数据存在磁盘 import redis #string ...
- Run Hyper-V and VirtualBox on the same machine (轉載)
Run Hyper-V and VirtualBox on the same machine Posted on September 5, 2012 by derek gusoff Recently ...
- linux 清除/var/spool/mail/root日志存储
检查出是/var/spool/mail下的root文件过大,然后进到服务器里面一查看,确实是这个问题,就是用cat /dev/null > /var/spool/mail/root,把这个文件清 ...
- 玩转ELK之三件套安装配置详解
ELK是啥子??? ELK 是elastic公司提供的一套完整的日志收集以及展示的解决方案,是三个产品的首字母缩写,分别是ElasticSearch.Logstash 和 Kibana. 特点: 收集 ...
- shell中得到当下路径所有文件夹名称
方法1: for dir in $(ls -al ./|awk '/^d/ {print $NF}') do echo $dir done 方法2: for dir in $(ls ./) d ...
- js复制文本
第一种: 自己测试时 只适合于input 和textarea 但是针对于其他标签的复制就不能用了.代码如下: <!DOCTYPE html> <html> <head&g ...