Decorators within classes
Topics: Decorators within a class Updated 2020-10-05 After having talked about decorators already on day 5 and day 6 I would like to revisit the topic to discuss how decorators can be used within classes. Let’s put ourselves in the position of a Castle Kilmere member during the time the school is in war with Master Odon and his Dark Army. So these are dark, scary times. People at Castle Kilmere are constantly scared that something might happen to them, their family or their friends.
Immutable data classes
Topics: Immutable data classes Updated 2020-10-05 We have talked a lot about data classes in the last post. There is one further characteristic of data classes that I would like to study - immutability. We can make a dataclass immutable such that it fulfills the same purpose as typing.NamedTuple. To make a dataclass immutable we have to set frozen=True when creating the class. Let’s see how we can change our DarkArmyMember class from typing.
Topics: Data classes Updated 2020-10-05 Data classes are a feature that is new in Python 3.7. And taking a look at them is definitely worth it! Data classes According to the PEP on data classes, they are basically “mutable namedtuples with defaults”. We already looked at namedtuples on day 10 and 11. Namedtuples allow us to create an immutable class that primarily stores values (i.e. attributes). We used namedtuples for our DarkArmyMember class (because once you become a member of the dark army there is no way back.
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.