Is Python's flexibility in the types of list elements a consequence of dynamic typing?

55 Views Asked by At

I am new to Python with some experience in C++. (Unfortunately, with just two sample points, any pair of features are either uncorrelated or perfectly correlated.) In Python, the elements in the same list can have any types. In C++, the STL containers hold homogeneous types. (I suppose it is possible to mimic the flexibility in Python lists with a vector of void pointers.) The C++ STL facilitates generic programming, but Python lists has far more genericty. What causes this contrast? Is it difficult to design a language with a static type system and have something like a Python list?

More generally, I often have to resist the urge to think "Python has feature A and C++ has feature B and therefore Python does X and C++ does Y." Are there good choices of languages that provide good comparison and contrast with these two so that I can understand what features of programming languages are correlated and what features are orthogonal issues? Does a formal education in computer science teach the analogy of linguistics to programming languages? (If so how can I learn that?)

1

There are 1 best solutions below

0
TrebledJ On

In Python, the elements in the same list can have any types. In C++, the STL containers hold homogeneous types. [...] The C++ STL facilitates generic programming, but Python lists has far more genericty. What causes this contrast? Is it difficult to design a language with a static type system and have something like a Python list?

The contrast is that Python uses duck typing, so it doesn't really care what you put into the list. On the other hand, C++ templates will figure out what types are being passed, instantiate the "skeleton" code with those types, then type-check the instantiated.

It is possible to have a heterogenous vector/list in C++. std::vector<void*> is one possibility you mentioned. With C++17 and above, a more type-safe alternative is std::vector<std::any> (more type-safe because std::any stores type information).

However, generally there are approaches which are more type-safe and less error-prone. If all you care about is your data, collecting different types of data under a single container, then you can consider the functional programming approach which is to use sum types or tagged unions (e.g. std::vector<std::variant<x, y, z>>). If you care about the element's data members and member functions, then you can consider the OOP approach which uses class-based/inheritance-based polymorphism.

Are there good choices of languages that provide good comparison and contrast with these two so that I can understand what features of programming languages are correlated and what features are orthogonal issues?

I will try to provide an objective answer to this.

Rarely will you see a PL stuff itself with every single feature. It's just unfeasible to maintain and apply in industry. PL maintainers and committees will make decisions and some feature proposals will be kicked out. Different PLs are built differently (e.g. some use the actor model for better concurrency, some use garbage collectors, some are designed more functionally). So just learn and expose yourself to different PLs. Choose from different paradigms (object-oriented, functional, concurrent).

C++ and Python are a good first step. Learning C++, you get an understanding of pointers, OOP, and types. Learning Python, you get a feel for how convenient and quick programming can be. Both languages are not without their weaknesses. While coding in Python, you might've realised: "Oh, I don't need to worry about pointers and memory leaks since it's all done under the hood!". But a few moments later: "Aiya, I returned the wrong type in this branch, why didn't Python warn me earlier?"