Interfaces.

P

PeterBraden1

Hi,

Does anyone know what the state of progress with interfaces for python
(last I can see is http://www.python.org/dev/peps/pep-0245/)

I would argue that interfaces/(similar feature) are necessary in any
modern language because they provide a way of separating the
specification from the implementation of a module.

I also had a new idea - when specifying the functionality of a module,
it would be nice to combine examples of valid behaviour / some sort of
testing.

It might therefore be possible to combine unit testing with
interfaces.

What do you all think?

Peter
(new to python so be nice :)
 
C

Chris M

Hi,

Does anyone know what the state of progress with interfaces for python
(last I can see ishttp://www.python.org/dev/peps/pep-0245/)

I would argue that interfaces/(similar feature) are necessary in any
modern language because they provide a way of separating the
specification from the implementation of a module.

I also had a new idea - when specifying the functionality of a module,
it would be nice to combine examples of valid behaviour / some sort of
testing.

It might therefore be possible to combine unit testing with
interfaces.

What do you all think?

Peter
(new to python so be nice :)

Status: Rejected

If you really want an "interface", make a class with methods that
raise NotImplementedError.
 
D

Dennis Lee Bieber

I would argue that interfaces/(similar feature) are necessary in any
modern language because they provide a way of separating the
specification from the implementation of a module.
Not from what I've seen... Java "interfaces" were created partly
because Java doesn't support multiple inheritance -- so an "interface"
is a way to indicate that a particular class ALSO supports another
class's "API". So... while <class X> inherits from <class Y>, the
interface allows <class X> to be used where ever <class Z> is used.

Python, OTOH, DOES have multiple inheritance. <class X> CAN inherit
I also had a new idea - when specifying the functionality of a module,
it would be nice to combine examples of valid behaviour / some sort of
testing.
Uh, isn't that already available via the two unit test schemes
already existing in Python?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

James Stroud

Chris said:
Status: Rejected

Thank you for pointing out the obvious. But *truly* helpful would be
insight into "While at some point I expect that Python will have
interfaces." Look for that sentence under the "rejected" part.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
 
D

davisn90210

I would argue that interfaces/(similar feature) are necessary in any
modern language because they provide a way of separating the
specification from the implementation of a module.

First of all, being a newbie, let me welcome you to python. I get the
feeling that you, like me, are coming from a strong background in
statically typed languages. Personally I have found that, while some
python ideology takes some time to get used to, once you get used to
the "python way", it is even more difficult going back. And it
provides a very rich set of features (overloading attribute access,
attribute descriptors, and function decorators come to mind) that are
simply unrivaled in any other language I have tried, combined with a
simple, clean, and clear syntax. Much of the reason why python does
certain things in certain ways has to do with a combination of power
and simplicity. Pythons dealings with interfaces are no exception.

Iterfaces are, and have always been, a part of every language. Even
before OO, there were interfaces. They may have been informally
defined (via documentation, word-of-mouth, etc.).

I know you are talking about "interfaces" in an OO sense, but in
understanding why python does not (yet) support the notion of a formal
"interface", it is important to realize that an "interface" is simply
a formal codification of an interface using the syntax of a language.

(Note that I will use "interfaces" to refer the formal interfaces,
while unquoted interface will refer to the broader meaning.)

In a statically-typed language, "interfaces" are very important. They
define what can, and can't, be done to an object. In dynamically-
typed languages, "interfaces" are less important. This is because
interfaces are not enforced at the language level. Instead,
interfaces are "enforced" through communication and documentation.

For example, at the language level, Python variables are simply
typeless "blobs" with attributes. Only when the code is actually
executed is there enough information to determine the type of a
variable. (It is possible to infer the type of a variable through
static analysis, but only in certain cases.)

Statically-typed languages, such as C++ or Java, rely on type
information in order to perform static checks that "ensure" all uses
of a variable within a program are "valid" to a point. For example,
you it should not be possible to obtain an "AttributeError" running
Java code that has successfully passed all the compile-time semantic
checking.

While it could be argued that statically-typed languages are better
than dynamically-typed languages, it is interesting to note that there
seems to be a fair amount of "working around" the type system in
Java. After all, there was a reason introspection capabilities were
added to Java. While static type-checking can catch a certain class
of errors, it is certainly not a panacea.

IMHO, one of python's greatest strengths is that it is dynamically
typed. I like to think of python having the power of C/C++ with the
ease and flexibility of scripting.

For example, one case where "interfaces" would get in the way is
overloading the [] (subscript) operator. In python, I just overload
it and run. I can pass those objects to *any* code which expect to
operate on objects through the [] operator and it just works.

In C++, I would have the following options:
1) Use templates. Templates are great, but they create their own
problems. For instance, they require a separate copy for every type
they are applied to.
2) Have all objects I would ever want to pass to the functions inherit
from a common class. Depending on the degree of flexibility I want,
this may range from sensible to arguably silly. In the extreme case,
I suppose, you would find yourself inheriting from some sort of a
"Subscriptable" class which merely flags that the class implements an
overloaded[]. IMHO, that is silly -- even more so when you consider
that various types could be used as the "index" ("IntSubscriptable",
"CharStarSubscriptable" anyone?).
And, of course, these work only if you wrote or can modify the code.
If you're using a library, your options for integrating it into your
framework is substantially limited.

Java, of course, doesn't support overloaded operators (much to its
loss, IMHO). However, you could easily come up with similar example
for it.

If I have digressed from the original discussion, I apologize.
However, I hope that the above gives you some insight into *why*
python doesn't (yet) have "interfaces". To summarize, there just
aren't that many usecases where they would provide much benefit
because the type model as a whole plays a lesser role in defining
interfaces. (And, as other posters have pointed out, "interfaces" can
be emulated using classes -- C++ doesn't have "interfaces" in the
sense that Java does either, although it does provide for pure virtual
methods.)

Hope this is helpful,

--Nathan Davis
 
B

Benjamin

Hi,

Does anyone know what the state of progress with interfaces for python
(last I can see ishttp://www.python.org/dev/peps/pep-0245/)

I would argue that interfaces/(similar feature) are necessary in any
modern language because they provide a way of separating the
specification from the implementation of a module.

I also had a new idea - when specifying the functionality of a module,
it would be nice to combine examples of valid behaviour / some sort of
testing.

It might therefore be possible to combine unit testing with
interfaces.

What do you all think?
Python is has duck typing. "If it quacks like a duke, it's duck." That
means that you look for what an object can do and has not what it is.
hasattr and getattr are used more than isinstance.
 
C

Chris M

Thank you for pointing out the obvious. But *truly* helpful would be
insight into "While at some point I expect that Python will have
interfaces." Look for that sentence under the "rejected" part.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com

Then answer his question yourself instead of wasting everyones
bandwidth going on some moral crusade. There's something to be said
about users with signatures longer than their messages...
 
S

Steven D'Aprano

Then answer his question yourself instead of wasting everyones bandwidth
going on some moral crusade. There's something to be said about users
with signatures longer than their messages...

Speaking of wasting bandwidth, please snip out the parts of the post
you're replying to that are no longer relevant. Also, you may notice that
James *did* answer the question, pointing out the relevant parts of the
PEP that you neglected to mention.

Note too that James' signature is seventy-three characters SHORTER than
his message.

There's something to be said about users who, having made an unhelpful
and almost pointless post, then follow it up by spewing venom and bile at
the person who made a gentle and reasonable comment about that
unhelpfulness.

I guess some people just can't take criticism, no matter how well-founded
or how gently it is put.
 
B

Bjoern Schliessmann

I would argue that interfaces/(similar feature) are necessary in
any modern language

I wouldn't, but have a look at the Zope interfaces module. It seems
to work similar to Java's interfaces.

Regards,


Björn
 
D

Duncan Booth

Bjoern Schliessmann said:
How do dukes quack, exactly? :)

Regards,

They quack with a North-eastern Scottish accent of course.

The following excerpt from "Scots: Practical Approaches" should make it
clear:
A. From Scotland the What?, by Buff Hardie, Stephen Robertson and
George Donald (Gordon Wright, 1987)

In this comic monologue from 1982, the owner of a toy shop in
Ballater, near Aberdeen telephones the Princess of Wales to ask what
her son would like for Christmas.

Noo, fit wid he like for his Christmas, the loon? Fit aboot a pair o’
fitba beets? Beets. Beets. B-O-O-T-S, beets. Weel, I ken that, but
he’ll surely grow intae them. Weel I’ll tell ye fit I’ve got. It’s
something very suitable. It’s oor ain special line in soft toys, and
it is a cuddly futret. A futret. Div ye nae ken fit a futret is?
Futret. F-E-R-R-E-T, futret. Now, cuddly futrets is exclusive tae the
Toy Shop, Ballater. We get them specially made up by a wee wifie, in
Hong Kong. Oh, an’ fit a job I hid explainin’ tae her fit a futret is.
Ye wid like a futret? Oh we’ll fairly manage ye a futret. Noo fit size
o’ a futret wid ye like? We’ve got a dinkie futret, a mini futret, a
life-size futret, a jumbo futret or a mega-futret. Ye’d like a jumbo
futret? No, it disnae hae a trunk. No, it’s got a string that ye pull,
an’ it sings Run, Rabbit, Run. Weel, fit else div ye expect a futret
tae sing? Now is there onythin’ else the loon wid like? Fit aboot a
rubber duke...for his bath? A duke. No, no, nae that kinda Duke.
D-U-C-K, duke. A quack quack duke. Like Donald Duke. Donald Duke. He’s
a freen’ o’ Mickey Moose...Moose...M-O-U-S-E, Moose! God, div ye nae
understan’ English, lassie?

This extract is interesting for a number of reasons. First of all,
obviously, it illustrates some of the stereotypical features of NE
Scots: the /f/ phoneme in ‘fit’, the /i/ in ‘beets’ and the /dj/ in
‘duke’, as in ‘Donald Duke’. Other features (such as the /u/ in
‘moose’ are shared with most other varieties of Scots. One obvious way
of approaching this text would be to ask what characteristics are true
of the pupils’ own variety of Scots, and what characteristics are not.

http://www.arts.gla.ac.uk/scotlit/asls/Scots_Practical_Approaches.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top