当前位置: 移动技术网 > IT编程>脚本编程>Python > python 的值传递和引用传递

python 的值传递和引用传递

2018年03月25日  | 移动技术网IT编程  | 我要评论

枪火天使无敌版,氯气安全规程,伤心城市简谱

python 的值传递和引用传递

虽然学习python不需要理解值传递和引用传递的概念,但是对于第一门语言为c语言的人来说,不搞清楚,总感觉别扭.

数值型数据

def test(a):
    a = 2
a = 3
test(a)
print "test:%d"%a
# 输出为test:3,可以看出对于数值型,是值传递

字符串数据

def test(a):
    a = "hi"
a = "hello"
test(a)
print "test:%s"%a
#输出 test:hello, 对于字符串来说,相当与值传递

数组数据

def test(a):
    a[0] = 2
a = [3]
test(a)
print "test:%d"%a[0]
#输出test:2, 对于数组,相当与引用传递

对象

class Peo(object):
    def __init__(self):
        self.name = "keith"
def test(a):
    a.name = "hi"
a = Peo()
test(a)
print "test:%s"%a.name
#输出 test:hi ,可以看出,对于对象,也是引用传递

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网