473,695 Members | 3,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Am I missing something with Python not having interfaces?

Studying OOP and noticed that Python does not have Interfaces. Is
that correct? Is my schooling for nought on these OOP concepts if I
use Python. Am I losing something if I don't use the "typical" oop
constructs found in other languages (Java, C# come to mind.) I'm
afraid that if I never use them I'll lose them and when I need them
for something beside Python, I'll be lost. Thank you.
Jun 27 '08 #1
28 1666
On May 6, 8:44*am, jmDesktop <needin4mat...@ gmail.comwrote:
Studying OOP and noticed that Python does not have Interfaces. *Is
that correct? *Is my schooling for nought on these OOP concepts if I
use Python. *Am I losing something if I don't use the "typical" oop
constructs found in other languages (Java, C# come to mind.) *I'm
afraid that if I never use them I'll lose them and when I need them
for something beside Python, I'll be lost. *Thank you.
In my school, we didn't even discuss the concept of interfaces (except
for CLI and GUI, that is). So I looked it up. I assume you are
referring to something like what's found here:

http://java.sun.com/docs/books/tutor...interface.html

If so, then it looks like an Interface is a generic class with method
stubs. You can do that with Python just as easily by creating empty
methods with just the "pass" keyword. It also reminds me of
Decorators...so you might want to look at those.

Since it's just a construct to implement polymorphism, I don't think
you'll lose anything. However, Python does not require you to re-
implement every method of the class it is inheriting from. You can
just override those that you want and leave the others alone.

Hopefully I understand this correctly...oth erwise, just ignore my
babbling and hand waving.

Mike
Jun 27 '08 #2
On May 6, 10:44*am, jmDesktop <needin4mat...@ gmail.comwrote:
Studying OOP and noticed that Python does not have Interfaces. *Is
that correct? *Is my schooling for nought on these OOP concepts if I
use Python. *Am I losing something if I don't use the "typical" oop
constructs found in other languages (Java, C# come to mind.) *I'm
afraid that if I never use them I'll lose them and when I need them
for something beside Python, I'll be lost. *Thank you.
Python supports interfaces. In the example below, "Vehicle" is an
interface.

class Vehicle:
def drive(self, count): raise Exception("I'm only an
interface... :-(")
def number_of_wheel s(self): return 0
def fly(self): pass

class Car(Vehicle):
def drive(self, count): print "The car walked %d steps" % count
def number_of_wheel s(self): return 4

As you can see, there are a couple of ways you can tell others
"Vehicle" is an interface, like raising exceptions, returning useless
values or doing nothing. You could also raise an exception in
Vehicle.__init_ _.

Jun 27 '08 #3
On 2008-05-06, jmDesktop <ne***********@ gmail.comwrote:
Studying OOP and noticed that Python does not have Interfaces. Is
that correct? Is my schooling for nought on these OOP concepts if I
Depends on your definition of 'Python does not have Interfaces'. They are not
in the official language, but they do exist. look into zope.interfaces , at
http://www.zope.org/Products/ZopeInterface .
use Python. Am I losing something if I don't use the "typical" oop
constructs found in other languages (Java, C# come to mind.) I'm
I think you are still thinking with a Java mind-set (no idea about C#, never
programmed with it).

Interfaces mainly exist to formalize TO A COMPILER that an object will provide
certain stuff. In this way, the compiler can catch such errors at compile time.

Python on the other hand does very little at compile time (other than parse
errors). Instead, at run-time it performs the checks that something you want to
use is actually there.
(in the same way that you don't declare variables a priori. You simply use them
and Python creates them for you when needed).

As a result, you get much more light-weight, more dynamic, code, which supports
the RAD nature of Python what makes it so much more productive.

(after a few years Python, you may find the Java way of doing things clunky).

afraid that if I never use them I'll lose them and when I need them
for something beside Python, I'll be lost. Thank you.
You can continue doing everything exactly in the way you do now. Effectively
you would then be programming Java/C# in Python syntax. I believe you would
gain very little by that move.

If you dare let go of your old habits, and embrace the mindset of a new
language, you can learn a lot more, namely that programming can be done in many
different ways (even if all those ways are called OOP).
If you only have a hammer, the whole world looks like a nail. If you have a
whole toolbox, problems become much more diverse (subtle). You learn to use
the right tools for the right problem, and maybe you find new (better) ways of
approaching old problems.
In the process, you may lose details of how something was done in language X,
but that's why they have invented books.

Sincerely,
Albert
Jun 27 '08 #4
jmDesktop wrote:
Studying OOP and noticed that Python does not have Interfaces. Is
that correct? Is my schooling for nought on these OOP concepts if I
use Python. Am I losing something if I don't use the "typical" oop
constructs found in other languages (Java, C# come to mind.) I'm
afraid that if I never use them I'll lose them and when I need them
for something beside Python, I'll be lost. Thank you.
--
http://mail.python.org/mailman/listinfo/python-list
Others have commented that interfaces are not normally important in Python.

However, if you want to do something *with* interfaces like declaring
that an object or class implements a particular interface, or querying
the interfaces implemented by an object, you might look at
zope.interface. Although it is used by and created for Zope,
zope.interface is packaged to be installable and usable outside of zope.
It's available at the Cheese Shop.

If you want to do adaptation from one interface to another or, for
example, register and look-up utilities by interface, zope.component is
also handy.

- Jim Washington
Jun 27 '08 #5
On May 6, 10:26*am, "A.T.Hofkam p" <h...@se-162.se.wtb.tue. nlwrote:
On 2008-05-06, jmDesktop <needin4mat...@ gmail.comwrote:
Studying OOP and noticed that Python does not have Interfaces. *Is
that correct? *Is my schooling for nought on these OOP concepts if I

Depends on your definition of 'Python does not have Interfaces'. They are not
in the official language, but they do exist. look into zope.interfaces , athttp://www.zope.org/Products/ZopeInterface.
use Python. *Am I losing something if I don't use the "typical" oop
constructs found in other languages (Java, C# come to mind.) *I'm

I think you are still thinking with a Java mind-set (no idea about C#, never
programmed with it).

Interfaces mainly exist to formalize TO A COMPILER that an object will provide
certain stuff. In this way, the compiler can catch such errors at compile time.

Python on the other hand does very little at compile time (other than parse
errors). Instead, at run-time it performs the checks that something you want to
use is actually there.
(in the same way that you don't declare variables a priori. You simply usethem
and Python creates them for you when needed).

As a result, you get much more light-weight, more dynamic, code, which supports
the RAD nature of Python what makes it so much more productive.

(after a few years Python, you may find the Java way of doing things clunky).
afraid that if I never use them I'll lose them and when I need them
for something beside Python, I'll be lost. *Thank you.

You can continue doing everything exactly in the way you do now. Effectively
you would then be programming Java/C# in Python syntax. I believe you would
gain very little by that move.

If you dare let go of your old habits, and embrace the mindset of a new
language, you can learn a lot more, namely that programming can be done inmany
different ways (even if all those ways are called OOP).
If you only have a hammer, the whole world looks like a nail. If you have a
whole toolbox, problems become much more diverse (subtle). You learn to use
the right tools for the right problem, and maybe you find new (better) ways of
approaching old problems.

In the process, you may lose details of how something was done in languageX,
but that's why they have invented books.

Sincerely,
Albert
I would imagine this is why I haven't found any schools teaching
Python in their basic programming classes too. On the dynamic typing,
isn't that the same sort of thing that lots of scripting languages
do? VBScript doesn't require you to define your variables, but I
don't really want to use it for anything (used to use it a lot in
Classic ASP.) I believe everyone that Python is great, but some of it
doesn't make sense to me as to why. Thanks.
Jun 27 '08 #6
I would imagine this is why I haven't found any schools teaching
Python in their basic programming classes too. On the dynamic typing,
isn't that the same sort of thing that lots of scripting languages
do? VBScript doesn't require you to define your variables, but I
don't really want to use it for anything (used to use it a lot in
Classic ASP.) I believe everyone that Python is great, but some of it
doesn't make sense to me as to why. Thanks.
Well, school-wise we got taught VB because it was easy for the non-
programmers. At university I learnt Java, C++, Haskell, Fortran and
Python. My favourite is obviously Python.

So here is my terrible (likely wrong) view of Python:

Python is a dynamic programming language, different to static ones
like Java or C++, and variables-wise, well you do have types, but it
is up to the interpreter to understand what to do with them. From my
understanding "variables" as you call them can be the following;
Integer 1, String "Hello", Tuple (), List [], Dictionary {} and a
higher level function (you can assign functions as varaible names,
that personally is amazing!).

Python is built to be easy to read, I think Guido (BDFL) said
something about 10% of the time code is written, 90% it is read, or
something to that affect. The point is that anyone who knows Python to
even a small degree should be able to pick up someone elses code and
given a small time understand the idea of it, even with any comments.

That is very powerful and key to the idea of Python, and my main love
for it. The issue I get from University is this idea that OOP is the
way to program, it is one of the ways, not the be all and end all. I
almost died when I got shown Haskell because nothing made sense, but
after using Python and returning, I understood more of the logic
because I had used the same techniques (in a more readable format) in
Python before.

A common bug people suffer from when trying a new language is doing
things how they would in their previous language of choice. Python is
not Java, or C++ or anything else other than Python. So read a few
tutorials on Python, see what people say it is good at and use it for
that purpose. I think it is an advantage to know how to do things
with different languages in different formats, but it is important to
know when to use one method over another. Personally DiveIntoPython
was a great guide for me as to the uses and benefits of Python, at a
basic level. Try googling it and reading the (free) articles and
tutorials online.

And welcome to Python, the grass is greener :)
Jun 27 '08 #7
jmDesktop <ne***********@ gmail.comwrites :
Studying OOP and noticed that Python does not have Interfaces. Is
that correct? Is my schooling for nought on these OOP concepts if I
use Python. Am I losing something if I don't use the "typical" oop
constructs found in other languages (Java, C# come to mind.) I'm
afraid that if I never use them I'll lose them and when I need them
for something beside Python, I'll be lost. Thank you.
You're not missing anything. An Interface is the Poor Man's Multiple
Inheritance. But in truth, even without multiple inheritance, Python
wouldn't need java-like interfaces.

--
Arnaud
Jun 27 '08 #8
co*********@gma il.com writes:
[...]
Python is built to be easy to read,
And also very easy to *write*. I rarely hear this, but it is the main
reason why I like Python so much. I can't really explain why though.

[...]

(cokofreedom, I found your explanation of the virtues of Python was
excellent!)

--
Arnaud
Jun 27 '08 #9
Hallöchen!

jmDesktop writes:
Studying OOP and noticed that Python does not have Interfaces. Is
that correct? Is my schooling for nought on these OOP concepts if I
use Python. Am I losing something if I don't use the "typical" oop
constructs found in other languages (Java, C# come to mind.) I'm
afraid that if I never use them I'll lose them and when I need them
for something beside Python, I'll be lost. Thank you.
See also http://www.python.org/dev/peps/pep-3119/

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber. org
(See http://ime.webhop.org for further contact info.)
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
2487
by: redefined.horizons | last post by:
I'm coming from a Java background, so please don't stone me... I see that Python is missing "interfaces". The concept of an interface is a key to good programming design in Java, but I've read that they aren't really necessary in Python. I am wondering what technique I can use in Python to get the same benefits to a program design that I would get with interfaces in Java. For example, if I want to have a program with a Car object, and...
0
8640
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9122
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9001
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4348
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2281
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.