Starting from the top:
def __init__(self):
self.tags = {}
__init__
is the constructor of the TagCloud
class. This means that every time I create a new instance of TagCloud
, this bit of code is run before anything else is done so that the instance is built correctly with all the proper attributes initialised.
If I declare an attribute in __init__
without passing it in at the time I instantiate it like here (in other words I make a call like tag_cloud = TagCloud()
when I create the instance) it only means that I do not pass an initial value so the class must make sure it is assigned something. Here we assign a {}
to self.tags
. This means self.tags
becomes an empty dictionary. If I declared my __init__
like this: def __init__(self, tags)
, it means that I must supply a value when I instantiate it (create a new instance) like this: tag_cloud = TagCloud(some_initial_value_for_tags)
. NB: When you pass a initial value like this the initialisation only happens once! At the time you create the instance.
The next bit of code:
def add(self, tag):
self.tags[tag] = self.tags.get(tag, 0) + 1
uses this self.tags
dictionary that you created in the __init__
. You would make the following call from your instance eg tags_cloud.add(some_tag_value)
. Unlike when you include this in the constructor like I showed above, you can do this every time you call tags_cloud.add(some_tag_value)
because you can call an instance’s methods as many times as you want but you can only create that instance once.
The following line of code does a lot of things and could be a bit confusing if you are new to python’s dictionary (dict
) types.
self.tags[tag] = self.tags.get(tag, 0) + 1
Starting with self.tags.get(tag, 0)
. self.tag
s is the dictionary object we declared in the constructor (__init__
). A dictionary is a collection where every entry in that collection has a unique key
that can appear only once in the entire collection. Every key has a value
and these key
value
“pairs” make up all the entries in the dict
collection.
So, I can access the value
in a number of ways, the first is self.tags[tag]
if you know that tag
is already a key
in the dictionary (tag
can be anything like “travis” or 0
or 23ab1f41
etc). If you do not know if it is in the dictionary, you can use the special method, get
. In python, dictionaries has this special method, get
(a dictionary is a class just like the classes you build here so it also has methods) which sends back the associated value
if that key already exists in the dictionary. You can supply a default value just incase it does not exist. This is what the 0
in self.tags.get(tag, 0)
does. So what happens here is that python will go look for a key
that has the same value that your tag
variable has. If it finds it, it will return that value, if it does not find it, it will return 0
.
So what the line of code does, is it takes your tag
, see if it is already a key
in your dictionary self.tags
. If it is, it will return the corresponding value and add 1
to it. If it does not exist, it will return 0
and then add 1
to it.
This is just a clever way to count the number of times you added (through tags_cloud.add
) that specific tag
to your dictionary.