Difference between these two methods of an user-defined class

Advertisements

I have no idea what the difference is between these two codes. When I rum those codes in the scoring python code, it marks mine wrong. I would appreciate it if you can tell me the different between using a variable to make a new empty list and appending values verses just making a list with values in it. The first code is mine and the second code is the code from the answer sheet. Thank you very much 🙂

class Server:

    def __init__(self):
        self.list = []

    def makeOrder(self,orderNum, orderList):
        existance = False
        for order in self.list:
            if order[0]==orderNum:
                existance = True
        if existance == True:
            return -1
        else:
            self.list.append([orderNum,orderList])
        return [orderNum,orderList]
class Server:

    def __init__(self):
        self.q = []

    # 1
    def makeOrder(self, orderNumber, orderList):
        existAlready = False
        for order in self.q:
            if order[0] == orderNumber:
                existAlready = True
        if existAlready == True:
            return -1
        else:
            tmp = []
            tmp.append(orderNumber)
            tmp.append(orderList)
            self.q.append(tmp)
            return tmp

>Solution :

Functionally, these are both very similar. The only difference of substance I see (other than obvious variable names, etc) is in the return value.

In the first option, you can see that one list is appended to self.list, and a new (albeit identical in value) list is returned. This means that this is a different object.

        self.list.append([orderNum,orderList])
    return [orderNum,orderList]

However in the second option, you can clearly see that tmp is both pushed AND returned:

        tmp = []
        tmp.append(orderNumber)
        tmp.append(orderList)
        self.q.append(tmp)
        return tmp

This is a single object that gets appended and returned.

Fundamentally, this mens that any modification to the returned list in the second option will be reflected inside self.q, while in the first option, the returned value is wholly independent. If you wanted to more or less replicate the behavior of option 1, you can change:

return tmp

with

return list(tmp)

Although keep in mind that if orderList is itself a mutable list, the same behavior will occur if you modify the returned value’s [1] element. Since it is a reference to a data structure, modification of that list will also affect self.q (or self.list in the first option)

Example:

>>> class Foo:
...     def __init__(self):
...             self.q = []
...     def bar(self, i, l):
...             tmp = [i, l]
...             self.q.append(tmp)
...             return list(tmp)
... 
>>> f = Foo()
>>> f.q
[]
>>> x = f.bar(1, [1,2,3])
>>> x
[1, [1, 2, 3]]
>>> x[1].append(4)
>>> x
[1, [1, 2, 3, 4]]
>>> f.q  # it changed here as well
[[1, [1, 2, 3, 4]]]

Leave a ReplyCancel reply