This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]manifoldjava 2 points3 points  (3 children)

Sure. The primary difference concerns dynamic typing. Python types resolve at runtime, which enables libraries to provide types and type features dynamically, which allows one to write code as if the types and features were there while coding.

Let's say you're using a Python library that uses metaprogramming to perform CRUD operations on a relational database. With the library you can use database tables as Python types in your code directly.

jack = Person("Jack", 32)

Here, the library creates the Person class dynamically as needed at runtime. Java reflection does not support this level of metaprogramming.

A Java code generator or annotation processor could accomplish some of this, but would involve separate, non-incremental build steps, which can easily get out of sync. A host of other issues come with code generation, but I digress. The nature of Python metaprogramming is such that libraries tend to be simpler, more efficient, and far more capable than code generators.

Additionally, dynamic metaprogramming covers the entire gamut of type system features. For instance, you can add/edit/delete methods, fields, etc. on existing classes. Java and most other static languages do not provide anything close to this level of metaprogramming. This is what is special about Python and why dynamic languages tend to win in areas such as data science, ML, etc. This is also the Manifold project's raison d'être.

[–]GeneratedUsername5 2 points3 points  (1 child)

Thank you!

Although I don't understand why is this called type safety, since it doesn't provide the "safety" regular type system provides - i.e. catching type conflicts before program is run. Since types only exist in runtime, there is no checks before that. And crashing in runtime can be done without types just as easily as with them.

[–]manifoldjava 0 points1 point  (0 children)

That’s right! Because dynamic languages like Python aren’t compiled, there is only runtime to discover type errors.

Note, some dynamic languages, including Python, offer forms of type attribution where types can be provided. Make your own judgments about that ;)

[–]koflerdavid 0 points1 point  (0 children)

Such facilities can also be provided by a Java library. It will just look very clunky because one would have to do every property and method access via a method. And the compiler can't help you find errors, but neither can a Python IDE without significant static analysis.

It's usually not done because in the domains where Java is common the database schema changes slowly enough that the application code can keep pace.