Abstract Base Classes
Topics: Abstract Base Classes, ABC’s Updated 2020-10-05 The last days I have been working on a lot of new classes and methods. Since they all belong to the same big concept, I decided to create one big post on the whole topic instead of several small ones. So let’s get right into it! Abtract Base Classes Up to now, our Magical Universe has a parent class (CastleKilmereMember) and several child classes (Pupil, Professor, etc.
Topics: Immutable classes, namedtuples Updated 2020-10-04 Tuples Before looking at namedtuples, we should review what a tuple is. In Python, a tuple is a simple data structure that can be used for grouping arbitrary objects. Important to know is that tuples are immutable. That means that once a tuple has been created, it can not be changed anymore. We already used tuples in our Magical Universe. For example, we defined the pet attribute of the Pupil class to be a tuple:
Duck typing, EAFP principle
Topics: Duck typing, EAFP principle Updated 2020-10-04 Duck typing Today we are going to look at some fundamental philosophies of programming: duck typing, EAFP and its opposite LBYL. Some of you may have heard of these principles already. However, because they are so fundamental we want to make sure that we apply them to our magical universe correctly. So what is duck typing? The most common saying regarding duck typing is “If it walks like a duck, and it quacks like a duck, then it must be a duck.
Extending the Magical Universe
Topics: Adding new classes and methods to the Magical Universe Today I used some of the concepts introduced during the last days to extend my Magical Universe. Specifically: I added a classmethod for creating the ghost ‘the mocking knight’ I added a ‘friends’ attribute to the Pupil class I added methods for adding and listing all current friends of a Pupil I implemented a new Charm class that allows to create charms I added two classmethods for creating popular charms
Underscore patterns for variable naming
Topics: underscore patterns for variable naming, _variable, __variable, __variable__, _ Updated 2020-10-04 Underscore patterns for variable naming Today we are going to look at a very important concept in Python: the usage of underscores when naming variables! In Python we have five different naming conventions that involve underscores. A variable with a single leading underscore like _elms This is a naming convention followed by most Python code A name prefixed with an underscore should be treated as a non-public part of the API and it might be changed without notice This holds independent of whether it is a function, a method or a data member A variable with (at least) two leading underscores and at most one trailing underscore like __my_private_variable
Properties, setter and getter methods
Topics: properties, @property and property(), setters, getters Updated 2020-10-04 Today, I digged a little deeper into the @property decorator, how it is related to the property() function and how its getter and setter methods work. These two links (link1, link2) were really helpful. Of course, there is also the official Python docs on the property() function. The @property decorator Yesterday we looked at decorators. The @property decorator allows us to create a read-only property.
Topics: decorators Updated 2020-10-04 Today, we are going to look at decorators. Python’s decorators are an advanced concept so don’t worry if you don’t immediately understand how they work. The more you will use and read about them, the clearer the concept will become. I won’t go into too much detail here, so if you want to know more about decorators, take a look at Dan Bader’s website or the PEP on decorators.
Topics: to-string conversion, __repr__, __str__ Updated: 2020-10-04 To-string conversion Today we are going to look at the two methods that control how an object is converted into a string object. When we just print an object, we won’t get a useful representation. For example, when trying to print the bromley instance: bromley = CastleKilmereMember(name='Bromley Huckabee', birthyear=1959, sex='male') print(bromley) We get this output: <__main__.CastleKilmereMember object at 0x7f81853bfc50>. It contains the name of the class an the ID of the object (its memory address).
Topics: Type annotations Updated 2020-10-04 Type annotations Type annotations are a very cool feature that came out with Python 3.5. They allow us to add arbitrary metadata to function arguments and the return value of a function. Why this is useful? First of all, it allows us to document of what type our function parameters are. Furthermore, they can be used for things like type checking. For more use cases, look here.
Types of class methods
Topics: class methods, instance methods, static methods, using class methods as alternative constructors Updated 2020-10-03 Types of methods A class can have three types of methods: instance methods, class methods and static methods. Instance methods are the most common type of method. They take at least the parameter self as an input. This parameter points towards an instance of the class when the method is called. An instance method can modify both object state (through the self parameter) and class state indirectly (through the self.
Topics: object oriented programming, classes, inheritance I want to start with using some of the things I learned from the “Python Tricks” book (see my reading list for more details on the book). Therefore, I will start creating a little Magical Universe with classes and methods related to the Tales of Castle Kilmere. So let’s start with the most important basics. What is Object Oriented Programming ? Object-oriented programming (OOP) is a specific programming paradigm.
100 Days of code - Creating my personal Magical Universe
I have decided to take on a new habit using a technique I found on the ‘get discplined’ subreddit. The technique works as follows: Take a piece of paper or card Draw 6 vertical lines intersecting 6 horizontal lines. This creates a 7x7 grid of 49 squares Choose a daily habit you want to work on and write it down on the back of the card Also put down WHY you want to work on the habit Start working on the habit TODAY!