passing in self data in python
Can you please clarify how it is that self.add(x)
below works the same way开发者_Python百科 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']
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.
self.add(x)
calls the instance method add
which in turn calls self.data.append(x)
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
.
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.
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.
精彩评论