Python language basics 87: reading from a text file


In the previous post we looked at context managers in with-blocks in Python. We said that context managers could be used with objects that require some type of opening and closing. We can open and close – or release – files. We can also open a HTTP channel to make a web request and then close it when we’re done. With-blocks makes our code more concise in that we don’t need to call the close() function anymore in code. Python will take care of that for us. In addition our code will be more robust since it can easily happen that the programmer forgets to call the close method on all open resources.

In this post we’ll look into reading text files. We’ll also see an example of multiple context managers.

Read more of this post

Python language basics 86: file I/O resource management in a context manager


In the previous post we went through a possible improvement of our first unreliable file I/O code using try-except-finally. The code is fine, it does what it is supposed to do. However, we’re still required to actively call the close function on the file handle. We’re dealing with resource management ourselves whereas we can ask Python to take care of it for us. If we forget to include the close method in our code then the file handle will stay open leading to all sorts of problems.

This is where context managers enter the scene. It’s a scary name that might at first imply a lot of complex code. We’ll in fact see that it makes our code shorter and more readable.

Read more of this post

Python language basics 85: file I/O resource management with try-finally


In the previous post we started looking into file operations in Python. We discussed various basic terms such as the file access mode and character encoding. We also went through some very basic code to write to a text file. We saw that the code wasn’t well designed. It is error-prone and not very reliable.

In this post we’ll present a way to make the code better with a traditional try-except-finally block.

Read more of this post

Python language basics 84: starting with file I/O


In the previous post we finished discussing the basics of object inheritance in Python. We saw an example where the Dog and a Duck classes inherited from an abstract base class called Animal. All common functionality was encapsulated within the Animal class. Dog and Duck retained the code specific to them.

In this post we’ll start looking into something very different: file input and output.

Read more of this post

Python language basics 83: polymorphism through inheritance III


In the previous post we started working on a scenario where class inheritance can be advantageous. We built two classes, Dog and Duck, and we saw that they had some similarities and also some unique features.

In this post we’ll see how to apply inheritance in code.

Read more of this post

Python language basics 82: polymorphism through inheritance II


In the previous post we looked at some theory behind inheritance. First we looked at the general meaning of the word ‘inheritance’ and then discussed its role in programming. We went through the basic terms such as superclasses, also knows as abstract classes or base classes, and derived classes.

In this post we’ll start building a simple example to make all of that clearer. If you’re have a background in strongly typed languages, like Java, then you’ll see that inheritance is implemented slightly differently in Python. However, the end result is much the same.

Read more of this post

Python language basics 81: polymorphism through inheritance


In the previous post we looked at interface polymorphism in Python. In fact there are no explicit interfaces in Python like in Java so I simply made up the term “interface polymorphism” referring to strictly typed languages.

We saw an example of two methods: one that printed the provided data in XML format, and another which performed the same in JSON format. The print_as_xml and print_as_json functions, which are also objects, implicitly implemented an interface with one method. This method is void, i.e. has no return type, and accepts two input parameters: a root name and the properties to be printed arranged in a dictionary. The Person object then had a format_me method which accepted a formatter and delegated the actual formatting implementation details to the mechanism that was passed into it. It’s important to keep in mind that the Person object remained oblivious of these implementation details and didn’t need to concern itself with the rules concerning JSON, XML, etc. Also if we pass in a function that doesn’t adhere to the implicit interface then an exception is thrown.

In this post we’ll start looking into another key idea in object-oriented design: inheritance. We’ll start with some theory and key terms. The upcoming posts will show the ideas in action.

Read more of this post

Python language basics 80: polymorphism through interfaces


In the previous post we started looking at one of the most important ideas of object oriented design: polymorphism. Polymorphism is a way to represent common features through objects. The implementation details of the related functions through those objects do not matter to the caller. E.g. all vehicles can be accelerated. In cars you press the gas pedal, you pedal faster on a bicycle and do something else on aeroplanes. The exact acceleration process is an implementation detail of the Car, Bicycle and Aeroplane classes probably represented by a function called “accelerate” that accepts some numeric argument, such as “how_fast”.

In this post we’ll look at interface type polymorphism in Python.

Read more of this post

Python language basics 79: polymorphism basics


In the previous post we discussed composite classes in Python. Composite classes are classes that are at least partly made up of other classes. We saw that there wasn’t anything special about adding other classes as class level properties to a class. In our example we constructed a Name, an Address and a Telephone classes that were all parts of the Person class.

In this post we’ll start looking into the key object-oriented idea of polymorphism. This is a relatively large topic and will span a couple of posts. In this post we’ll look at some theory first to pave the way for the code examples in upcoming posts.

Read more of this post

Python language basics 78: composite classes


In the previous post we looked at property getters in Python. They are “normal” methods but they have a special purpose, namely to read the values of class level properties. Getters and setters are not as strictly defined as in other popular languages like Java and C# and you’ll come across various solutions for those types of special functions in your Python projects.

In this post we’ll look at composite classes.

Read more of this post


A great site

Elliot Balynn's Blog

A directory of wonderful thoughts

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: