pass_data_test.py

# ===================================================================
# test when a variable is passed to a function is
# a reference or a value
#
# ===================================================================

def ChangeData(x1,x2):
    print('ChangeData({},{})'.format(x1,x2))
    x1 = x2
    return

print('-----pass-int------------------------------------------------')

i1 = 123
i2 = 456

print('i1={} {}'.format(i1,type(i1)))
print('i2={} {}'.format(i2,type(i2)))

ChangeData(i1,i2)

print('i1={}  i2={}'.format(i1,i2))


print('-----pass-float-----------------------------------------------')

f1 = 123.0
f2 = 456.0

print('f1={} {}'.format(f1,type(f1)))
print('f2={} {}'.format(f2,type(f2)))

ChangeData(f1,f2)

print('f1={}  i2={}'.format(f1,f2))


print('-----pass-bool-----------------------------------------------')

b1 = True
b2 = False

print('b1={} {}'.format(b1,type(b1)))
print('b2={} {}'.format(b2,type(b2)))

ChangeData(b1,b2)

print('b1={}  b2={}'.format(b1,b2))


print('-----pass-list-----------------------------------------------')

l1 = [1,2,3]
l2 = [4,5,6]

print('l1={} {}'.format(l1,type(l1)))
print('l2={} {}'.format(l2,type(l2)))

ChangeData(l1,l2)

print('l1={}  l2={}'.format(l1,l2))


print('-----pass-tuple----------------------------------------------')

t1 = ('a','b','c')
t2 = ('d','e','f')

print('t1={} {}'.format(t1,type(t1)))
print('t2={} {}'.format(t2,type(t2)))

ChangeData(t1,t2)

print('t1={}  t2={}'.format(t1,t2))


print('-----pass-object---------------------------------------------')

class xx:
    def __init__(self,d):
        self.data=d

    def getdata(self):
        return self.data

c1 = xx(100)
c2 = xx(200)

print('c1={} {}'.format(c1.getdata(),type(c1)))
print('c2={} {}'.format(c2.getdata(),type(c2)))

ChangeData(c1,c2)

print('c1={}  c2={}'.format(c1.getdata(),c2.getdata()))

print('---c1=c2-------------------------')
c1 = c2

print('c1={}  c2={}'.format(c1.getdata(),c2.getdata()))