0

Can you please clarify how it is that self.add(x) below works the same way as self.data.append(x)? That is, how does self.add(x) know to append to the list because we have not explicitly stated self.data.add(x)? When we state y.addtwice('cat'), 'cat' is added to 'self', not self.data.

class Bag:
    def __init__(self):
        self.data=[]
    def add(self,x):
        self.data.append(x)
        return self.data
    def addtwice(self,x):
        self.add(x)
        self.add(x)
        return self.data

>>> y = Bag()
>>> y.add('dog')
['dog']
>>> y.addtwice('cat')
['dog', 'cat', 'cat']

5 Answers 5

4

Because addtwice calls methods which are defined on self, and because self.data is a "mutable type", addtwice's call to add will end up appending the value of self.data. add, in turn calls self.data.append

When calling a function in a computer program, you can think of the process as being a series of substitutions like this:

# -> means (substitution for)
# <= means "return"
y = Bag()
y.add('dog') -> 
     y.data.append(x) ->
         #(machine code)
     <= y.data
# at this point, at the command propmt, python will just print what was returned.
y.addtwice('cat')->
     y.add('cat')->
         y.data.append(x) ->
             #(machine code) 
         <= y.data
     #nothing cares about this return
     y.add('cat')->
         y.data.append(x) ->
             #(machine code)
         <= y.data
     #nothing cares about this return either
     <= y.data
# at this point, at the command propmt, python will just print what was returned.

self, itself, is never really appended in any of those cases though. self.data is.

Sign up to request clarification or add additional context in comments.

2 Comments

@Saher I've been thinking a lot about language design of late, and this is how McCarthy originally proposed Lisp (and how Alonzo Church proposed Lambda Calculus).
Thanks very much, @cwallenpoole, for a detailed and clear explanation. I really appreciate it. To me, the key was learning that "add, in turn calls self.data.append" as GWW also noted. That is, I did not know how self.add and self.data.append were related.
1

self.add(x) calls the instance method add which in turn calls self.data.append(x)

Comments

1

When we state y.addtwice('cat'), 'cat' is added to 'self', not self.data

This is incorrect. cat is in fact added to self.data. Why would you think it was added to self?

y.add('dog') is the same as doing Bag.add(y, 'dog'). So add is really doing y.data.append('dog'), it's customary to use the name self instead.

y.addtwice('cat') is the same as doing Bag.addtwice(y, 'cat'). So addtwice is really doing y.add('cat') twice, which is the same as doing Bag.add(y, 'cat') twice. So addtwice is really doing y.data.append('cat') twice.

The self in each instance method is just an automatically added variable pointing to the instance it's called on, in this case y.

2 Comments

Thanks @agf for your reply. Yes, I said it incorrectly on purpose because that is what it seemed like it was doing. Before I got a reply to my question, I did not see why we could just say self.add(x) and the input would still be added to self.data. I would have thought we had to state self.data.append(x) for all the cases to be added to the list. That was why I asked for clarification.
Yep, that's why I explained it.
1

Let look at function add(self, x) from class Bag.

When that function is called, one of the parameter is self, which is the object itself, in this case, the same instance of Bag whose add function is called.

Therefore, in function add, calling self.data.append(x) is basically calling function append on data list of Bag, thus, adding the element x into the list.

Same thing for function addtwice. By calling function add twice, two elements are added into data list of Bag.

Both functions return the data list.

Comments

1

add(self, x) is just a function that you want to call.

append is a built in function that adds an element to the list.

so your add function basically uses append to add the element you want to the list and return the list you named data

self.addtwice will call self.add exactly two times and so will add the element twice.

1 Comment

Thank you, @Saher. You really helped to explain it for me by stating that "add(self, x) is just a function" and "append is a built in function". I got it. I got the difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.