Python中的OO? ^^ [英] OO in Python? ^^

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

问题描述




对不起我的无知,但在阅读了关于

python.org的Python教程之后,我有点惊讶关于python中缺少OOP

的能力。老实说,我甚至都没有看到关于OO如何在Python中实际工作的重点。


对于一个,有什么好的理由可以解释为什么我应该继承

班吗? ^^没有功能可以检查子类是否正确

实现了一个继承的接口,并且多态现象似乎也在Python中缺失了

。我无法想象在哪种情况下使用Python中的
继承有帮助。例如:


类基数:

def foo(个体经营):#我想说儿童必须实施foo

通过


class Child(Base):

pass#works


Python中的继承是否沸腾仅仅是代码共享?


我如何在Python中制定多态?例如:


class D1(Base):

def foo(self):

print" D1"


D2级(基础):

def foo(个体经营):

打印" D2"


obj = Base()#我想要一个多态的基类引用

if(< need D1>):

obj = D1()

其他:

obj = D2()


我还可以把整个继承的东西都拿出来并且程序

仍然可以工作(?)。


请给我希望Python仍然值得学习: - /


问候,

Matthias

Hi,

sorry for my ignorance, but after reading the Python tutorial on
python.org, I''m sort of, well surprised about the lack of OOP
capabilities in python. Honestly, I don''t even see the point at all of
how OO actually works in Python.

For one, is there any good reason why I should ever inherit from a
class? ^^ There is no functionality to check if a subclass correctly
implements an inherited interface and polymorphism seems to be missing
in Python as well. I kind of can''t imagine in which circumstances
inheritance in Python helps. For example:

class Base:
def foo(self): # I''d like to say that children must implement foo
pass

class Child(Base):
pass # works

Does inheritance in Python boil down to a mere code sharing?

And how do I formulate polymorphism in Python? Example:

class D1(Base):
def foo(self):
print "D1"

class D2(Base):
def foo(self):
print "D2"

obj = Base() # I want a base class reference which is polymorphic
if (<need D1>):
obj = D1()
else:
obj = D2()

I could as well leave the whole inheritance stuff out and the program
would still work (?).

Please give me hope that Python is still worth learning :-/

Regards,
Matthias

推荐答案

Matthias Kaeppler写道:
Matthias Kaeppler wrote:
<剪掉很多谈论仍在思考C语言的人>
<snip a whole lot of talk of someone still thinking in terms of C>




让这个启发你的方式,年轻的padawan:


modelnine @ phoenix 〜/ gtk-gnutella-downloads



Let this enlighten your way, young padawan:

modelnine@phoenix ~/gtk-gnutella-downloads


python

Python 2.4.2 (#1,2005年10月31日,17:45:13)

[GCC 3.4.4(Gentoo 3.4.4-r1,ssp-3.4.4-1.0,pie-8.7.8)]在linux2上

输入help,copyright,credit等等。或许可证或欲了解更多信息。
python
Python 2.4.2 (#1, Oct 31 2005, 17:45:13)
[GCC 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import this
Python的禅宗,Tim Peters


美丽胜过丑陋。

明确比隐含更好。

简单比复杂更好。

复杂优于很复杂。

平坦优于嵌套。

稀疏比密集更好。

可读性计数。

特价案件并不特别违反规则。

虽然实用性胜过纯洁。

错误不应该默默地传递。

除非明确地保持沉默。

面对模棱两可的情况,拒绝猜测的诱惑。

应该有一个 - 最好只有一个 - 明显的做法。

虽然这种方式起初可能并不明显,除非你是荷兰人。

现在总比没有好。

虽然从来没有比*更好对*现在。

如果实现很难解释,那就不错了。

如果实现很容易解释,那可能是个好主意。

命名空间是一个很棒的主意 - 让我们做更多的事情!
import this The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren''t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you''re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it''s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let''s do more of those!




并且,_do_(不只是阅读)教程,你会明白为什么你发布的

简短示例代码并不是pythonic,至少可以说:
http://www.python.org/doc/2.4.2/tut/tut.html

以及为什么Python中的继承是必要的,但是在一个完全不同的层面上

你在想什么。


哦,最后一点,如果你是德国人,你可以加入

de.comp.lang.python。


--- Heiko。



And, _do_ (don''t only read) the tutorial, and you''ll understand why the
short example code you posted isn''t pythonic, to say the least:
http://www.python.org/doc/2.4.2/tut/tut.html
and why inheritance in Python is necessary, but on a whole different level
of what you''re thinking.

Oh, and on a last note, if you''re german, you might as well join
de.comp.lang.python.

--- Heiko.


Matthias Kaeppler写道:
Matthias Kaeppler wrote:
Py中似乎缺少多态thon
polymorphism seems to be missing in Python




QOTW!


< / F>



QOTW!

</F>


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

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