Are Java and C # "customizable" like Python?

During the request, I use Python most of the time, and I did not use Java or C # much, although I have a (basic) idea of ​​how they work. I think that maybe I will start using Java or C # more, but it seems that from what I know about them, they are not as "configured" as Python, but I could be wrong.

By “customizable” (it might be better to use phrases to describe what I mean, but I can't think of anything better :-)), I mean things in Python, for example:

  • Defining attributes of dynamic objects (using __getattr__ , etc.)
  • Import hooks (therefore, code modules can be imported from any type of media, and not just from files that match certain sets of criteria) (see PEP 302 - New Imported Hooks and https://stackoverflow.com/a/422116/ ... )
  • Operator overloading (I think C # and Java have this, but this is another example)
  • Subclass of built-in types
  • Matching and modeling sequences using __getitem__ , __setitem__ and __delitem__ , etc., the "magic" methods.

So, I wonder if there are (at least some) these “settings” in Java and C #, and if not, are there any functionally similar or equivalent ways to do such things?

+4
source share
2 answers

Python, a fully dynamic language, will always have an advantage here in more static languages ​​such as C # and Java. Java and C # also traditionally target more conservative markets, which makes them more conservative in design. Although C # gets more and more dynamically oriented functions with each new version.

 Dynamic object attributes definition (using __getattr__, etc.) 

This can be done in C # 4.0 using the new dynamic support. Using IDynamicObject in C #, you can implement "method_missing" (ala Ruby). However, this is really against the general intention and “feeling” of the language, and I have never seen it in practice (with the possible exception of the ASP.NET MVC ViewBag)

 Operator overloading 

C # has operator overloading. However, in my experience, it is almost never used.

 Subclassing of built in types 

Only if the type is not sealed. If it is sealed, extension methods often do the trick. Many / most of the built-in types in C # are sealed.

  Mappings and sequence simulation using __getitem__, __setitem__, and __delitem__, etc., "magic" methods 

An IEnumerable<T> implementation is probably the best choice in C #. This will allow your type to connect to LINQ and get all kinds of goodies for "free"

I have very little experience with Python, my "dynamic language of choice" was Ruby. I also worked professionally in C # since it debuted. C # really went a long way and turned into a pretty decent language. He really has a “kitchen sink” that can be intimidating at the beginning. But for the most part, it blends well. However, the fluidity, flexibility, power, and potential for abuses that Ruby is not present in C #. Hardcore ruby-ist (and presumably python-ist) will almost certainly find C # frustrating and limiting. C # is still a very "corporate" language and always will be.

+4
source

"Are Java and C #" customizable "like Python?"

No, it is not.

In particular, Java, which is what I know best does not list the listed functions, expect to capture imports; compiled code can be loaded from any medium using a custom classloader.

There are some things that Java and C # have in their statically typed nature, such as detecting coding errors before running the code, but I think you already know this.

Therefore, referring to your question, this is not so. They are just different.

+2
source

Source: https://habr.com/ru/post/1341035/


All Articles