Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

python

c#

At the time of asking, I am using Python most of the time, and I have not used Java or C# much, although I have a (basic) idea of how they work. I am thinking to possibly start to use Java or C# more, but it seems from the little I know about them that they aren't as "customizable" as Python, but I may be wrong.

By "customizable" (there is probably better phrases to use to describe what I mean, but I can't think of anything better :-) ), I mean things in Python like:

  • Dynamic object attributes definition (using __getattr__, etc.)
  • Import hooks (so code modules can be imported from any type of media, not just files which match certain sets of criteria)(see PEP 302 -- New Import Hooks, and this Stackoverflow question)
  • Operator overloading (I think that C# and Java both have this, but it is another example)
  • Subclassing of built in types
  • Mappings and sequence simulation using __getitem__, __setitem__, and __delitem__, etc., "magic" methods

So, I am wondering if there are (at least some of) these "customization" kinds of things in Java and C#, and if not, are there functionally similar or equivalent ways to do these kinds of things?

like image 309
Abbafei Avatar asked Feb 23 '11 20:02

Abbafei


2 Answers

Python being a fully dynamic language is always going to have the advantage here over more static languages like C# and Java. Java and C# have also traditionally targeted more conservative markets, making them more conservative by design. Although C# gains more and more dynamic oriented features with each new release.

Dynamic object attributes definition (using __getattr__, etc.)

This can be accomplished in C# 4.0 using the new dynamic support. With IDynamicObject it's possible to implement "method_missing" (ala Ruby) in C#. However doing this is really against the language's overall intent and "feel", and I've never seen it done in practice (with the possible exception of ASP.NET MVC's 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 built in types in C# are sealed.

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

Implementing IEnumerable<T> is probably your best bet in C#. This will let your type hook into LINQ and get all kinds of goodies for "free"

I have very little experience with Python, my "dynamic language of choice" has been Ruby. I've also professionally worked in C# since it debuted. C# has really come a long ways and has grown into a pretty decent language. It does have a "kitchen sink" feel to it, which can be intimidating at first. But for the most part it does come together well. However, the fluidity, flexibility, power and potential for abuse that Ruby has is not present in C#. A hardcore ruby-ist (and presumably, python-ist) will almost certainly find C# frustrating and limiting. C# is still very much an "enterprise" language, and always will be.

like image 82
Matt Greer Avatar answered Sep 28 '22 08:09

Matt Greer


"Are Java and C# as “customizable” as Python?"

No, they aren't.

In particular Java which is what I know best lack of those features listed, expect for import hook; compiled code may be loaded from any media using a custom class loader.

There are some things Java and C# has though, by their statically typed nature, such as some coding mistakes detection before the code is run, but I guess you already know that.

So addressing your question, they are not. They're just different.

like image 32
OscarRyz Avatar answered Sep 28 '22 07:09

OscarRyz