Ikke's Blog

Post details: About the lack of Java-style interfaces in Python

Apr 15
About the lack of Java-style interfaces in Python

Just had some discussion about OO concepts, abstract classes and interfaces in Java and Python in #gnome-nl. I still dislike the fact Python and other interpreted languages got no enforced interface implementation support.

A little comparison (not meant to be disrespectful againt anyone):
When you're living in a Java world, you can go out on friday night, meet some person implementing the Girl interface, invite her at your place and have some fun (you know ;-)).

When you're Python, you might go out on friday night, meet some person who tells you "it" implements the Girl interface and at a first sight does so, as get_voice_timbre returns "girl" and has_boobs returns true. Then you take the person home, to your bedroom, and find out, well, it's a shemale.

Got the picture? :-)

Anyway... SoC news will follow later on!


Comment from: natrius [Visitor] Email
That's a NotImplementedException I hope I never come across.
PermalinkPermalink 04/15/07 @ 05:05
Comment from: Hugo [Visitor] Email
That is probably the worst analogy I've ever read...
PermalinkPermalink 04/15/07 @ 06:30
Comment from: Ronald S. Bultje [Visitor] Email · http://ronald.bitfreak.net/
Secueel gefrustreerd ofzo? :-).

Pak gewoon de ouderwetse aanpak: het is vrijdagavond, je gaat niet uit maar programmeert je eigen dame, a la Weird Science. Succes gegarandeerd!
PermalinkPermalink 04/15/07 @ 06:42
Comment from: Michael R. Head [Visitor] Email · http://www.core.binghamton.edu/~burner/
Interesting analogy, but of course it completely falls down because in Java, the whole point of the interface concept is to give you the power that you already had in a non-statically typed language (that is, the ability to treat something in a given way, no matter what it inherits.

Put another way, just because Person p implements the IGirl interface is no guarantee that it actually inherits all the internals of a proper Girl.

In any even, without static types, you do need to test a bit more (you can't just compile and assume it'll work), which IMHO is a good thing.
PermalinkPermalink 04/15/07 @ 07:30
Comment from: chapeaurouge [Visitor] Email · http://theopenstandard.com/blogs/chapeaurouge
Hehe... loving that comparison, made me laugh in this Sunday morning :) Well done :)
PermalinkPermalink 04/15/07 @ 08:45
Comment from: Jeff Walden [Visitor] Email · http://whereswalden.com/
So, honestly, don't you think that example's just a little bit too crass? On the overall scale it's fairly negligible, of course, but I highly suspect there are other people besides me who wish you had been (and would be, in the future) more careful with your words here. This definitely has the potential to blow up in your face if you get unlucky.
PermalinkPermalink 04/15/07 @ 09:11
Comment from: Marius Gedminas [Visitor] Email · http://gedmin.as
I think you need to look at zope.interface if you haven't already. Introspectable interfaces in Python (implemented as a C extension module for speed).
PermalinkPermalink 04/15/07 @ 11:14
Comment from: Ikke [Member] · http://www.eikke.com
Natrius: whole point is I'd love it if there was no chance a NotImplementedException would be thrown, as the "compiler" should mark the class pretending to implement some interface as invalid (or say "No you don't, don't pretend you do").

Hugo: too bad, you might be missing the point.

Ronald: zeker niet, het is geen persoonlijk verhaal ofzo he ;-) Waren erover bezig met stratos en JanC, en plots kwam dat in me op (ok, ik was niet helemaal nuchter). Leek me nog een leuke vergelijking, blijkbaar kunnen sommige mensen ook hier niet tegen :-/

Michael: I know you can check whether all things you want to be defined are actually defined, but that's just something I whish I shouldn't be forced to do. It's mainly a matter of personal taste.

chapeaurouge: Thanks :-)

Jeff: This is just an analogy, maybe somewhat "on the edge", but hey... I've read worse things before :-/
PermalinkPermalink 04/15/07 @ 11:17
Comment from: Ikke [Member] · http://www.eikke.com
Marius: I heard about it and it's installed on my system, I really should take a closer look. Thanks!
PermalinkPermalink 04/15/07 @ 11:19
Comment from: Markus [Visitor] Email · http://conic.se
You know. That object implementing the Girl interface could be an Adapter for some other "object"...

PermalinkPermalink 04/15/07 @ 12:13
Comment from: Ikke [Member] · http://www.eikke.com
Markus: damn, got me ;-)
PermalinkPermalink 04/15/07 @ 12:15
Comment from: Jesus M. Rodriguez [Visitor] Email · http://zeusville.wordpress.com
You assume that the object implementing Girl is not also implementing Male.
PermalinkPermalink 04/16/07 @ 01:17
Comment from: Ikke [Member] · http://www.eikke.com
Jesus: thát would be pretty confusing :s ;-)
PermalinkPermalink 04/16/07 @ 02:55
Comment from: Sebastian [Visitor] Email
Well, in Python you just have to trust the class. In Java you don't trust and make a contract.

In other words:

Python: Girlfriend
Java: Prostitute
PermalinkPermalink 04/16/07 @ 03:21
Comment from: Ikke [Member] · http://www.eikke.com
Glad to see some people seem to understand what I tried to say in this post and are able to produce a humorous reply :-)
PermalinkPermalink 04/16/07 @ 03:35
Comment from: ken [Visitor] Email
I've heard it explained as "C is all about types, Java is about types+interfaces, Python is all about the interface". So I guess to stretch an already taut analogy, "C is about DNA testing, Java is all about DNA testing + hotornot.com scores, and Python is all about hotornot.com scores".

In practice, I find the Python way easiest. You can always implement the Java way in Python if you want. (I've never seen anybody do this.) You can't easily implement the Python way in Java. And often it's really handy.

Of course, I (slightly) prefer the Lisp way, which is roughly "just like Python, except you can declare types like C/Java if you want" (but again, I've almost never seen this done).

It's great that people are questioning things (I've got my own list of Python pet peeves), but in this case I think the market has spoken: languages without Java-type-strictness are winning, and programmers in languages that allow both ways tend to not do the Java way. Sorry. But the SML people would probably love you. :-)
PermalinkPermalink 04/16/07 @ 07:56
Comment from: Walther [Visitor] Email
You are partly right, but in Java an object needs to implement the complete girl-interface (including headacheAtInconvenientTimes, talkAboutFeelings and expectExpensivePresents). A Python girl only needs to implement the features you like and/or need.

I'm happy to say I found myself a Python girl :)
PermalinkPermalink 04/16/07 @ 09:34
Comment from: Ikke [Member] · http://www.eikke.com
Walter: now I can't remember whether Java supports interface inheritance:
interface Girl
interface Girlfriend extends Girl

PermalinkPermalink 04/16/07 @ 11:52

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))


Who's Online?

  • Guest Users: 174


XML Feeds

What is RSS?