Python language basics 83: polymorphism through inheritance III

Introduction

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

Introduction

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

Reformatting extracted substrings using Match.Result in C# .NET

Say you have the following Uri:

http://localhost:8080/webapps/bestapp

…and you’d like to extract the protocol and the port number and concatenate them. One option is a combination of a regular expression and matching groups within the regex:

Read more of this post

Extending class definitions with partial classes in C# .NET

The ‘partial’ keyword helps you divide your classes into multiple files within the same namespace. One obvious usage of partial classes is to split the definition of a large type into smaller chunks. You cannot just use the partial keyword with classes but methods as well.

The partial classes will reside in two – or more – different cs files in the same namespace. Say you have a partial Customer class in the project-name/domains folder:

Read more of this post

Filtering exceptions in C# 6

There’s a new keyword in C# 6 which can only be used in conjunction with exception handling: when. With when we can put a filter on our catch clauses: catch a certain type of exception, e.g. IOException, but only if a certain condition is true. Otherwise continue with the next catch-clause if any.

Consider the following example:

Read more of this post

Importing static methods from a namespace in C# 6

You’ll probably know that if you’d like to use the simple class names in a C# file you have to import its namespace in a using directive. If you want to use the Task class without referring to its fully qualified name including the namespace you have to put…

using System.Threading.Tasks;

…in the top section of the file. Then you can write something like…

Task t = Task.Run<ReturnType>(() => ...);

Similarly the using statement…

using System;

Read more of this post

A new way to format strings in C# 6

In C# 5 we often format strings with the string.Format function. It requires a format string and then a number of parameters that are passed into the format string. C# 6 has a nice addition which simplifies that syntax.

Let’s start with the following Rockband object:

public class Rockband
{
	public string Name { get; }
	public int NumberOfMembers { get; }
	public decimal ConcertFee { get; }

	public Rockband(string name, int numberOfMembers, decimal concertFee)
	{
		Name = name;
		NumberOfMembers = numberOfMembers;
		ConcertFee = concertFee;
	}

	public string GetFancyName()
	{
		return Name.ToUpper();
	}
}

The fancy name is not too fancy but that’s not important now.

Read more of this post

Python language basics 81: polymorphism through inheritance

Introduction

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

Introduction

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

Getting rid of unnecessary using statements in Visual Studio 2015

Here comes a quick tip for all of you who are bothered by unnecessary using statements in a class file. You know when you have the automatic “using System.Linq” statement and you never use Linq in that class? It’s a breeze to get rid of those in Visual Studio 2015 using the light bulb icon:

Images

Expand the icon and select “Remove Unnecessary Usings”.

This feature has been available for a long time in NetBeans for Java developers. Visual Studio has finally caught up without having to resort to tools like ReSharper.

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

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