Python支持LSP,不是吗? [英] Python supports LSP, does it?
问题描述
维基百科
( http:/ /en.wikipedia.org/wiki/Python_...ed_programming )
说:
"""
Python对面向对象编程范式的支持是巨大的。它b / b
完全支持Liskov替换中的多态性
所有对象的原则意义。
"""
只是想知道它是否是真实的陈述。 LSP不是更高质量的类别层次而不是语言层次吗?评论?
AndyL
wikipedia
(http://en.wikipedia.org/wiki/Python_...ed_programming)
says:
"""
Python''s support for object oriented programming paradigm is vast. It
supports polymorphism [...] fully in the Liskov substitution
principle-sense for all objects.
"""
Just wondering if it is true statement. Is not LSP more a quality of the
desing of class hierachy rather then language itslef? Comments?
AndyL
推荐答案
Andy Leszczynski< leszczynscyATnospam.yahoo.com.nospam>写道:
Andy Leszczynski <leszczynscyATnospam.yahoo.com.nospam> writes:
维基百科
( http://en.wikipedia.org/wiki/Python_...ed_programming )
说:
"""
Python'对面向对象的支持编程范式是巨大的。它完全支持Liskov替换的多态性[/]所有对象的原则意义。
"""
只是想知道它是否是真实的陈述。 LSP不是更高质量的类层次设计而不是语言它的设计吗?评论?
wikipedia
(http://en.wikipedia.org/wiki/Python_...ed_programming)
says:
"""
Python''s support for object oriented programming paradigm is vast. It
supports polymorphism [...] fully in the Liskov substitution
principle-sense for all objects.
"""
Just wondering if it is true statement. Is not LSP more a quality of
the desing of class hierachy rather then language itslef? Comments?
这不是一个真实的陈述。语言中没有任何内容强制执行LSP。在
的事实上,甚至没有调用函数/方法来使
确保传入的类型是您期望的类型的子类型;还有
目前无法宣布那种类型。
它的功能是鸭子打字。您可以将任何对象传递给任何
函数/方法,只要它具有正确的
功能和属性,它就能工作。
维基百科真的滥用了LSP这个词。我已经更正了
维基百科。
< mike
-
Mike迈耶< mw*@mired.org> http://www.mired.org/home/mwm/
独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。
It''s not a true statement. Nothing in the language enforces LSP. In
fact, there''s not even a when a function/method is invoked to make
sure the type passed in is a subtype of the type you expect; there''s
currently no way to even declare what that type is.
What it does have is duck typing. You can pass any object to any
function/method, and it will work so long as it has the right set of
features and attributes.
The wikipedia was really abusing the phrase LSP. I''ve corrected the
wikipedia.
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Andy Leszczynski< leszczynscyATnospam.yahoo.com.nospam>写道:
Andy Leszczynski <leszczynscyATnospam.yahoo.com.nospam> writes:
LSP不是更高级的类层次设计的质量而不是语言itslef吗?评论?
我同意LSP更像是一个类层次结构而不是语言事物。
例如,在Python中编写LSP很容易 - 违规类:
类别基础:
def f(个体经营):
打印我是基础
班级孩子(基础):
f ="不是功能
既然你可以打电话给基地.f(),LSP说你也应该可以调用
child.f()。好吧,我想你可以,但大多数人不会考虑
抛出TypeError来满足LSP :-)。
Mike Meyer< mw * @ mired.org>写道:这不是一个真实的陈述。语言中没有任何内容强制执行LSP。事实上,当调用函数/方法来确保传入的类型是您期望的类型的子类型时,甚至没有[方式?]
Is not LSP more a quality of the desing of class hierachy rather then
language itslef? Comments?
I do agree that LSP is more a class hierarchy thing than a language thing.
For example, it''s easy in Python to write a LSP-violating set of classes:
class base:
def f(self):
print "I am base"
class child (base):
f = "not a function"
Since you can call base.f(), LSP says you should also be able to call
child.f(). Well, I suppose you can, but most people would not consider
throwing a TypeError to satisfy the LSP :-).
Mike Meyer <mw*@mired.org> wrote: It''s not a true statement. Nothing in the language enforces LSP. In
fact, there''s not even a [way?] when a function/method is invoked to make
sure the type passed in is a subtype of the type you expect
嗯,这不完全正确。我可以写:
def func(self,obj):
断言(isinstance(obj,baseClass))
它有点像非pythonic,但如果你真的想要这个语言肯定会让你这么做。
Well, that''s not entirely true. I could write:
def func (self, obj):
assert (isinstance (obj, baseClass))
It''s sort of un-pythonic, but the language certainly lets you do it if you
really want to.
2005年8月9日星期二17:36,Andy Leszczynski
< leszczynscyATnospam.yahoo.com.nospam> <>
(< hJ ******************** @ comcast.com>)写道:
On Tuesday 09 August 2005 17:36, Andy Leszczynski
<leszczynscyATnospam.yahoo.com.nospam> <>
(<hJ********************@comcast.com>) wrote:
维基百科
( http://en.wikipedia.org/wiki/Python_...ed_programming )说:
"""
Python对面向对象编程范式的支持是巨大的。它完全支持Liskov替换的多态性[/]所有对象的原则意义。
"""
只是想知道它是否是真实的陈述。
如果不是特别富有洞察力,这是真的。
这里想要的是以下替代品
属性[6]:如果对于S类型的每个对象o1,都有一个对象类型为
o2,这样对于按T定义的所有程序P,
当o1代替o2时,P的行为不变,那么
S是T的子类型。
不是LSP更质量的课堂设计而不是语言itlef?
在静态类型语言中,多态性基于类层次结构
(通过继承)。因为Python使用动态检查的类型,它的
多态性是结构而不是主格。
评论?
wikipedia
(http://en.wikipedia.org/wiki/Python_...ed_programming) says:
"""
Python''s support for object oriented programming paradigm is vast. It
supports polymorphism [...] fully in the Liskov substitution
principle-sense for all objects.
"""
Just wondering if it is true statement.
It''s true if not particularly insightful.
"What is wanted here is something like the following substitution
property [6]: If for each object o1 of type S there is an object
o2 of type T such that for all programs P defined in terms of T,
the behavior of P is unchanged when o1 is substituted for o2, then
S is a subtype of T."
Is not LSP more a quality of the desing of class hierachy rather then
language itslef?
In a statically-typed language, polymorphism is based on the class hierarchy
(through inheritance). Because Python uses dynamically-checked typing, its
polymorphism is structural rather than nominative.
Comments?
像这样的讨论可能更适合comp.object
新闻组。
Discussions like these might be more appropriate for the comp.object
newsgroup.
这篇关于Python支持LSP,不是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!