views:

506

answers:

6

To my surprise as I am developing more interest towards dynamic languages like Ruby and Python. The claim is that they are 100% object oriented but as I read on several basic concepts like interfaces, method overloading, operator overloading are missing. Is it somehow in-built under the cover or do these languages just not need it? If the latter is true are, they 100% object oriented?

EDIT: Based on some answers I see that overloading is available in both Python and Ruby, is it the case in Ruby 1.8.6 and Python 2.5.2 ??

+11  A: 

Thanks to late binding, they do not need it. In Java/C#, interfaces are used to declare that some class has certain methods and it is checked during compile time; in Python, whether a method exists is checked during runtime.

Method overloading in Python does work:

>>> class A:
...  def foo(self):
...    return "A"
...
>>> class B(A):
...  def foo(self):
...    return "B"
...
>>> B().foo()
'B'

Are they object-oriented? I'd say yes. It's more of an approach thing rather than if any concrete language has feature X or feature Y.

andri
+12  A: 

Dynamic languages use duck typing. Any code can call methods on any object that support those methods, so the concept of interfaces is extraneous. Python does in fact support operator overloading, as does Ruby.

Anyway, you seem to be focusing on aspects that are not essential to object oriented programming. The main focus is on concepts like encapsulation, inheritance, and polymorphism, which are 100% supported in Python and Ruby.

RossFabricant
+1  A: 

I can only speak for python, but there have been proposals for interfaces as well as home-written interface examples in the past.

However, the way python works with objects dynamically tends to reduce the need for (and the benefit of) interfaces to some extent.

With a dynamic language, your type binding happens at runtime - interfaces are mostly used for compile time constraints on objects - if this happens at runtime, it eliminates some of the need for interfaces.

Reed Copsey
+1  A: 

name based polymorphism

"For those of you unfamiliar with Python, here's a quick intro to name-based polymorphism. Python objects have an internal dictionary that contains a string for every attribute and method. When you access an attribute or method in Python code, Python simply looks up the string in the dict. Therefore, if what you want is a class that works like a file, you don't need to inherit from file, you just create a class that has the file methods that are needed.

Python also defines a bunch of special methods that get called by the appropriate syntax. For example, a+b is equivalent to a.add(b). There are a few places in Python's internals where it directly manipulates built-in objects, but name-based polymorphism works as you expect about 98% of the time. "

John Ellinwood
+1  A: 

Python does provide operator overloading, e.g. you can define a method __add__ if you want to overload +.

You typically don't need to provide method overloading, since you can pass arbitrary parameters into a single method. In many cases, that single method can have a single body that works for all kinds of objects in the same way. If you want to have different code for different parameter types, you can inspect the type, or double-dispatch.

Interfaces are mostly unnecessary because of duck typing, as rossfabricant points out. A few remaining cases are covered in Python by ABCs (abstract base classes) or Zope interfaces.

Martin v. Löwis
A: 

Have you read this? No?

Geo