5

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?

Community
  • 1
  • 1
Abbafei
  • 3,088
  • 3
  • 27
  • 24
  • Java does not have operator overloading, for sure. – corsiKa Feb 23 '11 at 20:28
  • Well, I'm not even a casual user of either language, but dynamic attribute retrival is very much against the spirit of static languages, so... –  Feb 23 '11 at 20:28
  • It has method overloading though. – Oliver Feb 23 '11 at 20:29
  • @delnan C#4 has dynamic types – Klaus Byskov Pedersen Feb 23 '11 at 20:31
  • @Klaus: Yes, but isn't most of the language, the standard library, general paradigm and programmers' mindsets rather static? And does C# have customizable attribute access, even if it only returns `dynamic` stuff? (And is it considered acceptable?) –  Feb 23 '11 at 20:33
  • 1
    Whilst C# can support dynamic operations now, if you dive in expecting to do lots of dynamic stuff you are missing the point of the language. Static languages and dynamic languages are different tools, sometimes for different jobs. There are advantages to both, but if you try to use a static language in a largely dynamic way you probably just end up with the worst of both worlds. – James Gaunt Feb 23 '11 at 20:39
  • What do you mean by "import hooks?" Most files being nothing more than byte streams does not limit Java from class loading from the network or shared memory, &c. Define more, please. – Jé Queue Feb 23 '11 at 20:45
  • @Xepoch: Clarified the part of the question about import hooks. – Abbafei Feb 23 '11 at 20:54

2 Answers2

5

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.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • 1
    Operator overloading is used a lot in C# but it's typically considered more of a commodity than a necessity. I recall seeing some graphics/math engine code using it extensively for vector and matrix operations. – anthonyvd Feb 23 '11 at 20:35
  • 1
    math related operations are one case where operator overloading can make sense. IMO though operator overloading is almost always a bad idea. – Matt Greer Feb 23 '11 at 20:37
2

"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.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569