I'm aware that a+=b and a=a+b do not always give the same result, depending on what they reference (correct me if I'm wrong here). I'm also aware of list aliasing issues in Python. see here: (Yet Another) List Aliasing Conundrum
The problem below seems to be neither of these, so I'm not sure what the issue is.
I have the following program. Pay special attention to the last line of add_clouds() and the last line of add_hosts().
define the globals and classes here
global REQUESTS
global CLOUDS
REQUESTS = []
CLOUDS = []
class Cloud:
def __init__(self, ID, coordinate, Hosts):
self.ID = ID
self.coordinate = coordinate # coordinate should be a tuple
self.Hosts = Hosts # Hosts should be a list of Host objects
class Host:
def __init__(self, ID, Resources, Cloud):
self.ID = ID
self.Resources = Resources # coordinate should be a tuple
self.Cloud = Cloud # Cloud object (NOT the Cloud ID)
This part generates the clouds and hosts
def add_cloud(ID,coordinate,Hosts=[]):
global CLOUDS
CLOUDS += [Cloud(ID, coordinate, Hosts)]
def add_host(Cloud_ID, Resources):
# search CLOUDS for Cloud_ID
Cloud = getCloud(Cloud_ID)
ID = len(Cloud.Hosts)+1
Cloud.Hosts += [Host(ID,Resources,Cloud)]
def getCloud(ID):
# returns cloud with ID provided
for cloud in CLOUDS:
if ID == cloud.ID:
return cloud
add_cloud(1,(10.7,13.5))
add_cloud(2,(1.8,3.0))
add_host(1,128)
Result in shell:
>>> CLOUDS
[<Cloud_Traversal.Cloud instance at 0x027336C0>, <Cloud_Traversal.Cloud instance at 0x02733DF0>]
>>> CLOUDS[1].Hosts
[<Cloud_Traversal.Host instance at 0x027334E0>]
>>> CLOUDS[0].Hosts
[<Cloud_Traversal.Host instance at 0x027334E0>]
>>>
You can see that the host is somehow added to both clouds, even though I only explicitly add it to one (in the line add_host(1,128)).
I've tried to see if this is an aliasing issue, but I don't think I'm breaking any rules here. Do you know what could be the problem?