Search code examples
pythonprogramming-languagescomputer-sciencetyping

What is the language feature that some kinds of classes are defined in terms of having some methods with special names?


This is a programming language concept question, e.g. similar to the level of Programming Language Pragmatics, by Scott.

In Python, the classes of some kinds of objects are defined in terms of having some methods with special names, for example,

  1. a descriptors' class is defined as a class which has a method named __get__, __set__, or __delete__().
  2. an iterators' class is defined as a class which has a method named __next__.

Questions:

  1. What is the language feature in Python called in programming language design? Is it duck typing?
  2. How does the language feature work underneath?
  3. In C++, C#, and Java, is it correct that a descriptor's class and an iterator's class would have been defined as subclasses of some particular classes? (similarly to C# interface IDisposable)

    In Python,

    • Can descriptors' classes be defined as subclasses of some particular class?

    • Can iterators' classes be defined as subclasses of some particular class?


Solution

  • What is the language feature in Python called in programming language design? Is it duck typing?

    "Any object with a member with a specific name (or signature), can work here" is duck typing. I don't think there is a more specific term for "any object with a member with a specific name (or signature), can work for this language feature", if that's what you were asking.

    How does the language feature work underneath?

    I don't understand the question. If a language feature means that it calls a method with a specific name, it calls a method with that name. That's it.

    In C++, C#, and Java, is it correct that a descriptor's class and an iterator's class would have been defined as subclasses of some particular classes?

    I'm not aware of anything similar to descriptor in any of these languages and I don't think it makes sense to speculate on how it would look if it did exist.

    As for iterators, each of these languages has a foreach loop, so you can look at that:

    In C++, the range-based for loop works on any type that has instance members begin and end or for which the begin and end functions exist. The returned type has to support the ++, != and * operators.

    In C#, the foreach loop works on any type that has instance method GetEnumerator(), which returns a type with a MoveNext() method and a Current property. There is also the IEnumerable<T> interface, which describes the same shape. Enumerable types commonly implement this interface, but they're not required to do so.

    In Java, the enhanced for loop works on any type that implements Iterable.

    So, there are no subclasses anywhere (C# and Java differentiate between implementing an interface and deriving from a base class). Java requires you to implement an interface. C# uses duck typing, but also optionally allows you to implement an interface. C++ uses duck typing, there is no interface or base class at all.

    Note that, depending on the language, the decision whether to use duck typing for a certain language feature might be complicated. As an extreme example, one feature of C# (collection initializers) requires implementing of a specific interface (IEnumerable) and also the presence of a method with a specific name (Add). So this feature is partially duck typed.