Week 13

Baptiste Higgs
Design Computing
Published in
4 min readJun 2, 2017

One column of my data in my open data project consists of a list of dates. There’s no ‘date’ class in python, so I had a few options:

  • Convert it into how many days have occurred since a set epoch day. This wasn’t a great option as in order to extract the year or the month or something else there would need to be a function written.
  • Convert each element in the list into a separate column of integers.
  • Create a new custom class specifically for these dates.

I ended up deciding to use a class due to how it was highly customisable, it was compact, and I wanted to try and learn more about classes in python, which I wasn’t (and still am not) too confident in.

Here’s some notes on what I’ve learnt:

  • Python is an object oriented programming language (OOPL), which means that you can structure objects in certain ways to help you achieve your goals. Although not as prevalent as in languages such as Java, these class definitions and how we can call them are what make Python an OOPL.
  • Classes begin with a ‘class classNameHere:’ line, a lot like a function, just starting with ‘class’ and without the brackets.
  • Inside the class different functions that can be called on the class are defined.
  • In the above class example, the function distance can be called from the class just like you call a function from an imported library like this:
  • First we defined the points, and then we used the ‘.’ to get into the classes functions, and called distance on it, using the other point as the term in our call of the function.
  • There are some funny functions for classes that can be defined too. For example, the __init__ function that you can see in the above code.
  • The __init__ function initialises the class. Basically, it tells python what to do when you want to make something in your class. In the above function, it takes in two arguments, and saves them as two separate floats under the same variable. If we want to access either of them, we can do so like this:
  • All classes in Python have to have an __init__ method, otherwise python won’t know what to do when we make a new instance of our class. Here’s the __init__ method used in the class I made for my open data project:
  • It takes in the day, month and year and sets them as different integer items of the same date variable instance.
  • There’s another definition that looks funny in the above code — the __str__() method. This defines how the function will react when I put it in str() function. I decided to print it like you would see most dates, in the DD/MM/YYYY format. The ability to define these sorts of things is one of the things that make class definitions so powerful.
  • One thing that you may have noticed is the presence of self in all the class’s function definitions. Self just refers to the instance of the variable you’re dealing with.
  • My current guess is that the double underscores on either side means that you’re dealing with overriding a function called that’s not specific to the function, however I am by no means an authoritative figure on the subject and there are a lot of different underscore configurations and uses. If you’re interested in how these work, I suggest you do further reading on the subject elsewhere. Here’s an article I found that may be useful:

There’s always more to python than I thought when I do a little experimenting!

--

--