About Python distutils

Hello World,

This post is about Python distutils.  You may wonder, “why distutils?” and not some other Python packaging framework such as setuptools.  The reason is that distutils is distributed with Python and it is good enough for simple Python modules.  Python offers a few options for packaging, and this can be confusing.  That is the other factor in choosing the defacto library that is installed with Python 2 and Python 3.

When first starting with distutils, I landed on the Distributing Python Modules page on docs.python.org, which includes this introduction. This is a great resource, but a little daunting for a newcomer.  I left still wondering how to use distutils.

I also found this tutorial for distutils, which included a good outline of the folder structure, but was still a little confusing.

I then found these instructions for packaging Python libraries, which was more complete and had good practical advice.

 

C++ Question – Can you have a virtual destructor?

Can you have a virtual destructor?

Yes, and if you have a base class, then it should have a virtual destructor.  The scenario where virtual destructors are needed is:

User allocates derived class instance using new() and assigns it to a pointer variable of the base class.

NonVirtualDestructor* c = new DerivedNonVirtualDestructor();
delete c;

In this situation, when delete is called, the destructor for the NonVirtualDestructor class will be called, but not the destructor for the DerivedNonVirtualDestructor class.  This could cause havoc if the DerivedNonVirtualDestructor class is expecting its destructor to always be called (e.g. to free previously allocated resources).  See item 14 in Effective C++ by Meyers for a more detailed explanation of why you should always make sure base classes have virtual destructors.

Here is a gist demonstrating virtual destructors, and the pitfall described above.

Posted in C++