【Python】python学习笔记——pass del

python学习笔记——passdel

(2009-07-30 09:34:18)
标签:

python

pass

del

引用

it

分类: Python

Let’s take a quick lool at two statements:pass and del

<wbr></wbr>

1 pass: 空语句什么也不执行,用处颇广。

<wbr></wbr>

if name =='Ralph Auldus Melish':

print 'Welcome'

elif name == 'Enid':

#not finish yet ...

pass

elif name == 'Bill Gates':

print 'Access Denied'

<wbr></wbr>

此时,因为某些原因,我们还不知道第一个elif要做什么,所以就先用pass代替,这样调试程序可以跳过这个elif 继续执行下面的。

<wbr></wbr>

2 del<wbr> 删除</wbr>

<wbr></wbr>

看这个例子:

<wbr></wbr>

>>> x = 1

>>> del x

>>> x

<wbr></wbr>

Traceback (most recent call last):

File "<pyshell#6>", line 1, in <module>

x

NameError: name 'x' is not defined

>>> x = ['Hello','world']

>>> y = x

>>> y

['Hello', 'world']

>>> x

['Hello', 'world']

>>> del x

>>> x

<wbr></wbr>

Traceback (most recent call last):

File "<pyshell#12>", line 1, in <module>

x

NameError: name 'x' is not defined

>>> y

['Hello', 'world']

>>>

<wbr></wbr>

可以看到x和y指向同一个列表,但是删除x后,y并没有受到影响。这是为什么呢?The reason for this is that you only delete the name,not the list itself,In fact ,there is no way to delete values in python(and you don’t really need to because the python interpreter does it by itself whenever you don’t use the value anymore)

<wbr></wbr>

举个例子,一个数据(比如例子中的列表),就是一个盒子,我们把它赋给一个变量x,就是好像把一个标签x贴到了盒子上,然后又贴上了y,用它们来代表这个数据,但是用del删除这个变量x就像是把标有x的标签给撕了,剩下了y的标签。

<wbr></wbr>

这就是Python的变量引用思想。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值