Reputation: 83
From Learn Python the Hard Way:
Python sees you mentioned mystuff and looks up that variable. It might have to look backwards to see if you created with =, look and see if it is a function argument, or maybe it's a global variable. Either way it has to find the mystuff first.
Once it finds mystuff it then hits the . (period) operator and starts to look at variables that are a part of mystuff. Since mystuff is a list, it knows that mystuff has a bunch of functions.
It then hits append and compares the name "append" to all the ones that mystuff says it owns. If append is in there (it is) then it grabs that to use. Next Python sees the ( (parenthesis) and realizes, "Oh hey, this should be a function." At this point it calls (aka runs, executes) the function just like normally, but instead it calls the function with an extra argument.
That extra argument is ... mystuff! I know, weird right? But that's how Python works so it's best to just remember it and assume that's alright. What happens then, at the end of all this is a function call that looks like: append(mystuff, 'hello') instead of what you read which is mystuff.append('hello').
Where does he get "mystuff" from? And I'm still unsure about how that period operator thing works (sorry I'm new at this please bear with me), later on we get this:
ten_things = "Apples Oranges Crows Telephone Light Sugar"
print "Wait there's not 10 things in that list, let's fix that."
stuff = ten_things.split(' ')
I don't see how that string becomes a list after the last line, does the .split automatically turn it into one or what? What is the name of that period "split" or "append" thing he's doing? One of the main things screwing me up in programming is that I don't know what a lot of things are actually called. I know functions, variables, etc but some stuff like that .split just confuse me.
Help?
Upvotes: 4
Views: 1558
Reputation: 8711
Regarding “Where does he get "mystuff" from?”, mystuff
is an object of some kind, and there are methods or functions among the object's attribute values (or among the attribute values of its class). The dot (period) is a qualifier operator; for example, mystuff.append
qualifies or identifies the relevant append
function to be the one associated with object mystuff
. Object methods typically have an implicit argument (often called self
) as the first argument, and that argument is made equal to the object the method belongs to. In this case, that's mystuff
.
As mentioned in a previous answer, split
splits a string and returns a list. For more information, also see tutorialspoint regarding split
:
The method split() returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num. ... Following is the syntax for split() method:
str.split(str="", num=string.count(str))
.
Upvotes: 2
Reputation: 1755
stuff = ten_things.split(' ')
doesn't change the value of ten_things
. Instead, it creates a new variable named stuff
and saves the list created by ten_things.split(' ')
to it. The space passed as an argument to the split
method here is significant. What it is saying is that Python should take the string ten_things
and split it up, using split
s argument as a delimiter.
Example:
"This is a string".split(' ') == ["This", "is", "a", "string"]
or
"This|is|a|string".split('|') == ["This", "is", "a", "string"]
Upvotes: 4