Python“implements< interface>”当量? [英] Python "implements <interface>" equivalent?

查看:134
本文介绍了Python“implements< interface>”当量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我最近开始使用Python,并且非常满意

如何高效,即使是新用户也是如此。我希望使用我的研究语言继续

,并且遇到了一些

绊脚石。


我是一名经验丰富的Java程序员,也是界面的忠实粉丝...

ie如果我创建了许多不同的类,那么

的实现接口X,我可以将它们全部作为参数传递给函数

或者需要X对象的东西。


Python中有类似的东西吗?


我想做的是为我的工作创建一个特征检测系统

- 具体来说,是一个名为Feature的通用类/接口。然后

子类在所有

中以不同且独特的方式实现像isFeaturePresent()这样的函数。我很想听听我怎么能这样做。

Python。


谢谢,

Wojciech

Hi,

I recently started using Python and am extremely happy with how
productive it''s made me, even as a new user. I''m hoping to continue
using the language for my research, and have come across a bit of a
stumbling block.

I''m a seasoned Java programmer and quite a big fan of interfaces...
i.e. The idea that if I make a number of distinct classes that
implement interface X, I can pass them all as parameters to functions
or whatnot that require an X object.

Is there something similar in Python?

What I''d like to do is create a feature detection system for my work
-- specifically, a general class / interface called "Feature" and then
subclasses that implement functions like isFeaturePresent() in all of
their different and unique ways. I''d love to hear how I can do this in
Python.

Thanks,
Wojciech

推荐答案

Wojciech Gryc napisa3(a):
Wojciech Gryc napisa3(a):

我是经验丰富的Java程序员和相当大的接口粉丝...

即如果我创建了许多不同的类,即
实现接口X,我可以将它们全部传递给函数的参数

或者需要X对象的东西。


Python中有类似的东西吗?
I''m a seasoned Java programmer and quite a big fan of interfaces...
i.e. The idea that if I make a number of distinct classes that
implement interface X, I can pass them all as parameters to functions
or whatnot that require an X object.

Is there something similar in Python?



我看到的最接近的是zope.interface。

The closest thing I saw is zope.interface.


我想做什么为我的工作创建一个特征检测系统

- 具体来说,是一个名为特征的通用类/接口。然后

子类在所有

中以不同且独特的方式实现像isFeaturePresent()这样的函数。我很想听听如何在
Python中做到这一点。
What I''d like to do is create a feature detection system for my work
-- specifically, a general class / interface called "Feature" and then
subclasses that implement functions like isFeaturePresent() in all of
their different and unique ways. I''d love to hear how I can do this in
Python.



我相信你不需要接口来用Python做这些事情。

Duck typing"就是我们如何称呼这个功能。 :)


-

Jarek Zgoda

Skype:jzgoda | GTalk: zg***@jabber.aster.pl |声音:+48228430101

我们读过Knuth所以你不必这么做。 (Tim Peters)

I am sure you wouldn''t need interfaces to do such things in Python.
"Duck typing" is how we call this feature. :)

--
Jarek Zgoda
Skype: jzgoda | GTalk: zg***@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don''t have to." (Tim Peters)


10月4日上午11点11分,Wojciech Gryc< wojci ... @ gmail.comwrote:
On Oct 4, 11:11 am, Wojciech Gryc <wojci...@gmail.comwrote:




我最近开始使用Python,我非常高兴

生产它让我如何,即使一个新用户。我希望使用我的研究语言继续

,并且遇到了一些

绊脚石。


我是一名经验丰富的Java程序员,也是界面的忠实粉丝...

ie如果我创建了许多不同的类,那么

的实现接口X,我可以将它们作为参数传递给函数

或者需要X对象的东西。


Python中有类似的东西吗?
Hi,

I recently started using Python and am extremely happy with how
productive it''s made me, even as a new user. I''m hoping to continue
using the language for my research, and have come across a bit of a
stumbling block.

I''m a seasoned Java programmer and quite a big fan of interfaces...
i.e. The idea that if I make a number of distinct classes that
implement interface X, I can pass them all as parameters to functions
or whatnot that require an X object.

Is there something similar in Python?



是的:你这样做的方式与用Java做的几乎相同,除了

两个不同之处:

*省略了implements接口部分

*实际上并没有创建一个接口类


然后,瞧!,你可以编写可以接受任何类的函数

实现你的界面。


Yes: you do it pretty much the same way you''d do it in Java, except
for two differences:
* leave out the "implements Interface" part
* don''t actually create an Interface class

And then, voila!, you can write functions that can accept any class
that implements your interface.


我想做的是为我的工作创建一个特征检测系统

- 具体地说,是一个名为Feature的通用类/接口。然后

子类在所有

中以不同且独特的方式实现像isFeaturePresent()这样的函数。我很想听听如何在
Python中做到这一点。
What I''d like to do is create a feature detection system for my work
-- specifically, a general class / interface called "Feature" and then
subclasses that implement functions like isFeaturePresent() in all of
their different and unique ways. I''d love to hear how I can do this in
Python.



只需在您想要的任何类中定义isFeaturePresent()。这就是你所需要的所有b $ b b;任何定义此方法的类都可以传递给任何调用它的

函数,而不必实现。或者子类

任何东西。


这就是所谓的鸭子打字。用Python术语。

Carl Banks

Just define isFeaturePresent() in any class you want. That''s all you
have to do; any class that defines this method can be passed to any
function that invokes it, without having to "implement" or "subclass"
anything.

This is known as "duck typing" in Python lingo.
Carl Banks


谢谢Carl,谢谢Jarek。这让我感觉好多了 -

编码,我会去。 :)


再次感谢,

Wojciech


10月4日上午11:27,Carl Banks< ; pavlovevide ... @ gmail.comwrote:
Thank you Carl and thank you Jarek. This makes me feel much better --
on to coding, I shall go. :)

Thanks again,
Wojciech

On Oct 4, 11:27 am, Carl Banks <pavlovevide...@gmail.comwrote:

10月4日上午11点11分,Wojciech Gryc< wojci ... @ gmail.comwrote:
On Oct 4, 11:11 am, Wojciech Gryc <wojci...@gmail.comwrote:


Hi,


我最近开始使用Python并且非常满意

富有成效,即使是作为新用户,也让我感到高兴。我希望使用我的研究语言继续

,并且遇到了一些b
绊脚石。
I recently started using Python and am extremely happy with how
productive it''s made me, even as a new user. I''m hoping to continue
using the language for my research, and have come across a bit of a
stumbling block.


我是一位经验丰富的Java程序员,也是界面的忠实粉丝......

ie如果想法我创建了许多不同的类,即
实现接口X,我可以将它们全部作为参数传递给函数

或者需要X对象的东西。
I''m a seasoned Java programmer and quite a big fan of interfaces...
i.e. The idea that if I make a number of distinct classes that
implement interface X, I can pass them all as parameters to functions
or whatnot that require an X object.


Python中有类似的东西吗?
Is there something similar in Python?



是的:你这样做的方式与用Java做的几乎相同,除了

两个不同之处:

*省略了implements接口部分

*实际上并没有创建一个接口类


然后,瞧!,你可以编写可以接受任何类的函数

实现你的界面。


Yes: you do it pretty much the same way you''d do it in Java, except
for two differences:
* leave out the "implements Interface" part
* don''t actually create an Interface class

And then, voila!, you can write functions that can accept any class
that implements your interface.


我想做的是为我的工作创建一个特征检测系统

- 具体地说,称为特征的通用类/接口。然后

子类在所有

中以不同且独特的方式实现像isFeaturePresent()这样的函数。我很想听听如何在
Python中做到这一点。
What I''d like to do is create a feature detection system for my work
-- specifically, a general class / interface called "Feature" and then
subclasses that implement functions like isFeaturePresent() in all of
their different and unique ways. I''d love to hear how I can do this in
Python.



只需在您想要的任何类中定义isFeaturePresent()。这就是你所需要的所有b $ b b;任何定义此方法的类都可以传递给任何调用它的

函数,而不必实现。或者子类

任何东西。


这就是所谓的鸭子打字。在Python术语中。


Carl Banks


Just define isFeaturePresent() in any class you want. That''s all you
have to do; any class that defines this method can be passed to any
function that invokes it, without having to "implement" or "subclass"
anything.

This is known as "duck typing" in Python lingo.

Carl Banks



这篇关于Python“implements&lt; interface&gt;”当量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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