加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

【Python有坑系列】函数参数传递:传值?引用?

发布时间:2020-12-17 01:24:55 所属栏目:Python 来源:网络整理
导读:p style="margin-left:10px;" 一、首先要说的是:变量 与 对象 p style="margin-left:10px;"在python中,类型属于对象,变量是没有类型的,这正是python的语言特性,也是吸引着很多pythoner的一点。所有的变量都可以理解 是内存中一个对象的“引用”,或者,

<p style="margin-left:10px;">一、首先要说的是:变量 与 对象

<p style="margin-left:10px;">在python中,类型属于对象,变量是没有类型的,这正是python的语言特性,也是吸引着很多pythoner的一点。所有的变量都可以理解 是内存中一个对象的“引用”,或者,也可以看似c中void*的感觉。所以,希望大家在看到一个python变量的时候,把变量和真正的内存对象分开。

<p style="margin-left:10px;">类型是属于对象的,而不是变量。这样,很多问题就容易思考了。

<p style="margin-left:10px;">例如:?

<p style="margin-left:10px;">nfoo = 1?? #一个指向int数据类型的nfoo(再次提醒,nfoo没有类型)

<p style="margin-left:10px;">lstFoo = [1]?? #一个指向list类型的lstFoo,这个list中包含一个整数1。

<p style="margin-left:10px;">接着说例子好了:

<p style="margin-left:10px;">def ChangeInt( a ):

<p style="margin-left:10px;">????? a = 10? # change the number

<p style="margin-left:10px;">nfoo = 2?

<p style="margin-left:10px;">ChangeInt(nfoo)

<p style="margin-left:10px;">print nfoo #结果是2

<p style="margin-left:10px;">这时发生了什么,有一个int对象2,和指向它的变量nfoo,当传递给ChangeInt的时候,按照传值的方式,复制了变量nfoo的值,这样,a就是nfoo指向同一个Int对象了,函数中a=10的时候,发生什么?

<p style="margin-left:10px;">(还记得我上面讲到的那些概念么),int是不能更改的对象,于是,做了一个新的int对象,另a指向它(但是此时,被变量nfoo指向的对象,没有发生变化),于是在外面的感觉就是函数没有改变nfoo的值,看起来像C++中的传值方式。

<p style="margin-left:10px;">def ChangeList( a ):

<p style="margin-left:10px;">????? a[0] = 10? # change the number

<p style="margin-left:10px;">lstFoo = [2]

<p style="margin-left:10px;">ChangeList(lstFoo )

<p style="margin-left:10px;">print nfoo #结果是[10]

<p style="margin-left:10px;">注:<a href="http://www.cnblogs.com/bovine/archive/2011/11/26/2264390.html" rel="nofollow">原文出处

<p style="margin-left:10px;">二、点调用与参数传递

<p style="margin-left:10px;">对象调用固有方法可以用点调用,自定义函数调用需要参数传递。

<p style="margin-left:10px;">?

<pre class="has">
<code class="language-python">>>> testlist = [['1','2009-03-02',3.762146,-8],['1','2009-03-01',3.758066,'2009-01-03',3.76085332653061,['0','2009-12-28',39.759765375,-104.984303125],'2009-12-27',39.70999875,-104.95675168749999],'2009-12-24',39.72202824,-104.96074968],'2009-12-23',39.77230825,-105.0435074375],'2009-12-21',39.77455175,-105.01426466666665],'2009-12-20',39.76241472,-104.90343176000002],['299','2009-08-14',29.894691,-81.314517],'2009-08-13',29.894691000000005,]

testlist.listToClass()
Traceback (most recent call last):
File "<pyshell#40>",line 1,in
testlist.listToClass()
AttributeError: 'list' object has no attribute 'listToClass'
listToClass(testlist)

三、类元素可修改

虽然int 是不可更改的对象,作为类的属性时却可以更改。

紧跟上述代码

<pre class="has">
<code class="language-python">>>> def change_m1(nodelist):
nodelist.m_1+=1

change_m1(testlist[0])
testlist[0].m_1
1

四、函数内做的列表整体修改该传递不出来,但是可以改变局部列表元素

<pre class="has">
<code class="language-python">>>> def changlist(list1,list2):
list1 = [1,1]
list2 = [2,2]

list1 =[5,6]
list2=[]
changlist(list1,list2)
list1
[5,6]
list2
[]

<pre class="has">
<code class="language-python">>>> def changelist2(list1,list2):
list1[0] = 1
list2[0] = 2

list1=[0,0]
list2=[1,1,1]
changelist2(list1,list2)
list1
[1,0]
list2
[2,1]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读