我错过了没有接口的Python吗? [英] Am I missing something with Python not having interfaces?

查看:88
本文介绍了我错过了没有接口的Python吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习OOP并注意到Python没有接口。

是否正确?如果我使用Python,我的学校教育对于这些OOP概念是徒劳的。如果我不使用典型的话,我会失去一些东西吗? oop

在其他语言中找到的结构(Java,C#浮现在脑海中。)我是

害怕如果我从不使用它们我会失去它们我需要他们

除了Python之外的东西,我会迷失方向。谢谢。

解决方案

5月6日,8:44 * am,jmDesktop< needin4mat ... @ gmail.comwrote:


学习OOP并注意到Python没有接口。 *

是否正确? *如果我使用Python,我的学校教育对于这些OOP概念是徒劳的。 *如果我不使用典型的话,我会失去一些东西吗? oop

在其他语言中找到的结构(Java,C#浮现在脑海中。)*我是

如果我从不使用它们会害怕我会失去它们当我需要它们时,除了Python之外的某些东西,我会迷失方向。 *谢谢。



在我的学校,我们甚至没有讨论接口的概念(除了

用于CLI和GUI,即)。所以我查了一下。我假设你是

指的是像这里发现的东西:

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


如果是这样,那么看起来接口是一个泛型类,方法是

stubs。只需使用传递方法创建空的

方法,就可以轻松地使用Python。关键词。它还让我想起了

装饰器...所以你可能想看看那些。


因为它只是一个实现多态的构造,我不认为

你会失去任何东西。但是,Python不要求你实现
实现它继承的类的每个方法。你可以

只是覆盖你想要的那些并留下其他人。


希望我理解正确...否则,只要忽略我的

bab呀学语和挥手。


Mike


5月6日,10:44 * am,jmDesktop< ; needin4mat ... @ gmail.comwrote:


学习OOP并注意到Python没有接口。 *

是否正确? *如果我使用Python,我的学校教育对于这些OOP概念是徒劳的。 *如果我不使用典型的话,我会失去一些东西吗? oop

在其他语言中找到的结构(Java,C#浮现在脑海中。)*我是

如果我从不使用它们会害怕我会失去它们当我需要它们时,除了Python之外的某些东西,我会迷失方向。 *谢谢。



Python支持接口。在下面的示例中,车辆是指车辆。是一个

的界面。


类车辆:

def drive(自我,计数):提高异常(我只有一个

界面...: - (")

def number_of_wheels(个体经营):返回0

def fly(self ):通过


级车(车辆):

def drive(自我,计数):打印车走了%d步%count

def number_of_wheels(个体经营):返回4


如你所见,有几种方法可以告诉别人

车辆是一个界面,比如提高异常,返回无用的

值或什么也不做。你还可以在

车辆.__ init__中引发异常。


2008-05-06,jmDesktop< ne *********** @ gmail.comwrote:


学习OOP并注意到Python没有接口。

这是正确的吗?如果I


取决于你对''Python没有接口'的定义。它们不是官方语言的b $ b,但确实存在。查看zope.interfaces,
http://www.zope.org / Products / ZopeInterface


使用Python。如果我不使用典型的话,我会失去一些东西吗? oop

在其他语言中找到的结构(Java,C#浮现在脑海中。)我是



我想你还在考虑一个Java思维模式(不知道C#,永远不会用它编程
)。


接口主要用于形式化对象将提供的TO A COMPILER

某些东西。通过这种方式,编译器可以在编译时捕获这样的错误。


另一方面,Python在编译时做的很少(除了解析

错误) )。相反,在运行时,它执行检查,你想要实际使用的是


(就像你没有声明变量一样您只需使用它们

并且Python会在需要时为您创建它们。


因此,您可以获得更轻量级,更具动态性,代码,支持

Python的RAD性质使它更有效率。


(几年后Python,你可能会发现Java做事笨重的方式。


如果我从不使用它们会害怕我会失去它们当我需要它们时

除了Python之外的东西,我会迷失方向。谢谢。



您可以按照现在的方式继续完成所有操作。实际上,您将使用Python语法编写Java / C#编程。我相信你会因为这一举动而获得很少的收入。


如果你敢于放弃旧习惯,并接受新的思维方式/>
语言,你可以学到更多东西,即编程可以用很多方式完成(即使所有这些方法都称为OOP)。

如果你只有一把锤子,整个世界就像钉子一样。如果你有一个完整的工具箱,问题变得更加多样化(微妙)。你学习使用

正确的工具来解决正确的问题,也许你会找到新的(更好的)方法来接近旧问题。

在这个过程中,你可能会丢失在X语言中完成某些事情的细节,但这就是他们发明书籍的原因。

此致,

Albert


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.

解决方案

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...otherwise, just ignore my
babbling and hand waving.

Mike


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_wheels(self): return 0
def fly(self): pass

class Car(Vehicle):
def drive(self, count): print "The car walked %d steps" % count
def number_of_wheels(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__.


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


这篇关于我错过了没有接口的Python吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆