What is the point of using abstract methods?

Say you have three printers that you need to write drivers for, Lexmark, Canon, and HP. All three printers will have the print() and getSystemResource() methods. However, print() will be different for each printer, and getSystemResource() remains the same for all three printers. You also have another concern, you would like to apply polymorphism. Since … Read more

Can you cache a virtual function lookup in C++?

There are two costs to a virtual function call: The vtable lookup and the function call. The vtable lookup is already taken care of by the hardware. Modern CPUs (assuming you’re not working on a very simple embedded CPU) will predict the address of the virtual function in their branch predictor and speculatively execute it … Read more

Abstract UserControl inheritance in Visual Studio designer

What we want First, let’s define the final class and the base abstract class. public class MyControl : AbstractControl … public abstract class AbstractControl : UserControl // Also works for Form … Now all we need is a Description provider. public class AbstractControlDescriptionProvider<TAbstract, TBase> : TypeDescriptionProvider { public AbstractControlDescriptionProvider() : base(TypeDescriptor.GetProvider(typeof(TAbstract))) { } public override … Read more

Is it possible to make abstract classes?

Use the abc module to create abstract classes. Use the abstractmethod decorator to declare a method abstract, and declare a class abstract using one of three ways, depending upon your Python version. In Python 3.4 and above, you can inherit from ABC. In earlier versions of Python, you need to specify your class’s metaclass as … Read more

Determine if a Python class is an Abstract Base Class or Concrete

import inspect print(inspect.isabstract(object)) # False print(inspect.isabstract(MessageDisplay)) # True print(inspect.isabstract(FriendlyMessageDisplay)) # True print(inspect.isabstract(FriendlyMessagePrinter)) # False This checks that the internal flag TPFLAGS_IS_ABSTRACT is set in the class object, so it can’t be fooled as easily as your implementation: class Fake: __abstractmethods__ = ‘bluh’ print(is_abstract(Fake), inspect.isabstract(Fake)) # True, False