I would like to get some advice on my problem with the below code. I declared a few global variables (such as shelfDim
, a
, b
, c
, d
, productList
). These global variables were then used as default arguments in my 2 functions (packByLength
, packByWidth
).
As part of the function packByWidth()
, some of the arguments were altered. For instance, I created a new list newDim
, which is basically a copy of the productList
. The function then modifies this list by swapping a few elements around. The problem is, my global variables then get changed as a result of running this function. Hence, calling packByWidth()
once gives the correct answer, but calling it a second time gives me the wrong answer (because the global variables got changed the first time it was called).
I know that for python, when
a=[1,2,3]
b=a
b[0]=2
both a
and b
will be changed to [2,2,3]
. However, i was not expecting this to happen as my list operations were contained inside a function.
Could anyone tell me what is causing this behaviour, and what can be done to prevent this from happening? I would like my global variables to remain unchanged regardless of how many times I run my functions! P.s. I have tried using tuples in my global variables but it has not helped. Thank you!
import numpy as np
# Parameters
shelfDim = [100, 120, 150]
a = [15, 4, 30]
b = [11, 8, 12]
c = [5, 3.5, 18]
d = [23, 6.8, 10]
productList = [a, b, c, d]
# 1 All products to be aligned length-wise (assuming product must be placed upright)
def packByLength(products = productList, shelfDim = shelfDim):
usableProdSpace =list(shelfDim)
usableProdSpace[0] = usableProdSpace[0]/len(products)
totalQty = []
for product in products:
packableQty = []
for dim in range(3):
packableQty.append(usableProdSpace[dim]//product[dim])
totalQty.append(packableQty)
return totalQty
# 2 All products to be aligned width-wise (assuming product must be placed upright)
def packByWidth(products = productList, shelfDim = shelfDim):
swap = None
newDim = list(products)
for product in newDim:
swap = product[0]
product[0] = product[1]
product[1] = swap
print ('products', products)
print ('newDim', newDim)
totalQty = packByLength(products = newDim)
return totalQty