可选的静态打字 [英] Optional Static Typing

查看:62
本文介绍了可选的静态打字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向Python添加可选的静态类型看起来像一个相当复杂的
的东西,但也很有用:
http://www.artima.com/weblogs/viewpost.jsp?thread=85551


我只有几个笔记:


Boo( http://boo.codehaus.org/ )是一种不同的语言,但我喜欢它的

" as而不是:而不是和 - >,要有:

def min(a as iterable(T))为T:

而不是:

def min(a:iterable(T)) - > T:

有时将静态打字和零件混合在一起可能很有用

没有它在同一个模块中(因为动态打字很有用
$ b有时是$ b),但有些时候你想确保有一个完整的

类型和检查模块。所以也许一种编译器指令(比如用于定义编码的#b $ b)可用于完全定义一个模块,并且类似于pychecker完全检查
。 (带有静态输入的模块
输入对于真正的python编译器也非常有用。)

熊抱抱,

熊宝宝

Adding Optional Static Typing to Python looks like a quite complex
thing, but useful too:
http://www.artima.com/weblogs/viewpost.jsp?thread=85551

I have just a couple of notes:

Boo (http://boo.codehaus.org/) is a different language, but I like its
"as" instead of ":" and "->", to have:
def min(a as iterable(T)) as T:
Instead of:
def min(a: iterable(T)) -> T:
Sometimes it can be useful to mix parts with static typing and parts
without it in the same module (because dynamic typing is very useful
sometimes), but some other times you want to be sure to have a full
typed and checked module. So maybe a kind of compiler directive (like
the # used to define encoding) can be used to define a module fully
typed and fully checked by a pychecker-like. (A module with static
typing can be very useful for a "true" python compiler too.)
Bear hugs,
bearophile

推荐答案

be************@lycos.com 写道:
向Python添加可选静态类型看起来像一个相当复杂的东西,但很有用也是:
http://www.artima。 com / weblogs / viewpost.jsp?thread = 85551

感谢指出Guido van Rossum的那篇文章。看起来好像

今天刚出来。它可以添加到Python 3.0中:
http:/ /www.python.org/moin/Python3.0

有时将静态打字和零件混合在一起可能很有用
没有它在同一模块中(因为动态打字非常有用
有时),但有些时候你想确保有一个完整的
打字和检查模块。


现在CPython中最接近的选项是使用Pyrex。
http://nz.cosc.canterbury.ac.nz/~greg/python/Pyrex/


Boo( http://boo.codehaus.org/ )与众不同语言,但我喜欢它的
as而不是:而不是和 - >,有:def min(a as iterable(T))为T:
而不是:
def min(a:iterable(T)) - > T:
Adding Optional Static Typing to Python looks like a quite complex
thing, but useful too:
http://www.artima.com/weblogs/viewpost.jsp?thread=85551
Thanks for pointing out that article by Guido van Rossum. Looks like it
just came out today. It is something that may be added to Python 3.0:
http://www.python.org/moin/Python3.0
Sometimes it can be useful to mix parts with static typing and parts
without it in the same module (because dynamic typing is very useful
sometimes), but some other times you want to be sure to have a full
typed and checked module.
The closest option right now in CPython is to use Pyrex.
http://nz.cosc.canterbury.ac.nz/~greg/python/Pyrex/

Boo (http://boo.codehaus.org/) is a different language, but I like its
"as" instead of ":" and "->", to have:
def min(a as iterable(T)) as T:
Instead of:
def min(a: iterable(T)) -> T:




对,你是第一个例子是Boo是如何做到的,第二个例子是Guido

如何做到这一点在Python 3.0中。


Boo翻转问题。而不是可选的静态类型,

默认情况下一切都是静态类型的(但是类型推断所以

你并不总是需要明确声明类型),而且它有

可选的运行时类型,类似于python'('duck typing")。
http://svn.boo.codehaus.org/trunk/te....boo?view=auto
http://svn.boo.codehaus.org/trunk/ex....boo?view=auto
http://svn.boo.codehaus.org/trunk/te....boo? view = auto
http://boo.codehaus.org / Duck +打字


还有这样做的一些缺点。这意味着Python

比Boo更灵活,正如我几个月前所说:
http://groups-beta.google.com/group/...51cdc9ffecbc72



Right, you''re first example is how Boo does it, and the 2nd is how Guido
proposed to do it in Python 3.0.

Boo flips the problem around. Instead of optional static typing,
everything is statically typed by default (but with type inference so
you do not always need to explicitly declare the type), and it has
optional runtime typing that works like python''s ("duck typing").
http://svn.boo.codehaus.org/trunk/te....boo?view=auto
http://svn.boo.codehaus.org/trunk/ex....boo?view=auto
http://svn.boo.codehaus.org/trunk/te....boo?view=auto
http://boo.codehaus.org/Duck+Typing

And there are some disadvantages to doing it this way. It means Python
is more flexible to use than Boo, as I stated a couple months back:
http://groups-beta.google.com/group/...51cdc9ffecbc72




< be ************ @ lycos.com>在消息中写道

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

<be************@lycos.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
向Python添加可选的静态类型看起来像一个相当复杂的东西,但也很有用:
http://www.artima.com/weblogs/viewpost.jsp?thread=85551

其中一条评论在Artima问一个相当深刻的问题:静态打字是一个答案。问题是什么?

(这是一个意思。)


每个人似乎给出的答案就是它

可以防止错误并澄清程序。


我不相信它对任何一个目标都有效。

我的观点对于那些通常使用测试第一次编程的人来说,这是个b $ b(如测试驱动开发或极限编程)。

很多当你去写一个测试的学科,然后

完全写出测试通过所需的代码时,所谓的优点根本就没有了。 />
不是一次击键更多。


静态打字所谓的大多数错误

要抓住根本就不要坚持超过一分钟

你做测试驱动开发。


这并不是说TDD是最重要的全部

正确性。正式方法和正式检查

都有很好的声誉。事实上,具有最佳声誉的技术

体现在旧IBM的每个办公室的墙上都是一个标志:想想!


所以让我们看起来更深一点。据我所知,静态

打字来自于70年代的正式方法工作,像Djikstra,Hoar和Wirth这样的人(仅限于/>
a少。)问题是,如果你正确使用他们提倡的正式程序

派生方法,那么你不需要

它:你的程序将是正确的,因为它可以获得

而不是你被提升为神。


所以这里的结论是静态打字是一个尝试

,以便为那些不应该首先编程的人安全编程。这可能听起来有些愤世嫉俗,但大多数真正的超级程序员在他们的背景中都有

Lisp或Smalltalk,并且

经常都是。这两种语言都没有

有静态打字,他们根本就不需要它。


所以如果问题是减少错误,那么可能

我们应该在错误

出现的地方工作。


有时提出的另一点是它'对于通过反思提供类型信息非常有用。$ / b
我曾经认为这是一个有效的问题,直到我开始在PyFit上工作
。我不得不在程序中加入一个简单的元数据设施,以取代没有类型信息的
替换,并且我发现它是非常有用的其他

你无法通过反映

类型数据获得的东西。


John Roth

熊抱,
bearophile
Adding Optional Static Typing to Python looks like a quite complex
thing, but useful too:
http://www.artima.com/weblogs/viewpost.jsp?thread=85551
One of the comments on Artima asks a rather profound
question: static typing is an answer. What''s the question?
(That''s a paraphrase.)

The answer that everyone seems to give is that it
prevents errors and clarifies the program.

I''m not convinced that it''s all that effective at either objective.
My viewpoint on this is
that of someone who generally uses test first programming
(as in Test Driven Development or Extreme Programming).
Many of the supposed advantages simply aren''t there
when you go to the discipline of writing a test and then
writing exactly the code needed to make the test pass, and
not one keystroke more.

Most of the kinds of error that static typing is supposed
to catch simply don''t persist for more than a minute when
you do test driven development.

This isn''t to say TDD is the be-all and end-all of
correctness. Formal methods and formal inspections
both have very good reputations. In fact, the technique
with the best reputation is embodied in a sign that was
on the wall of every office of the old IBM: Think!

So let''s look a bit deeper. As far as I remember, static
typing came out of the formal methods work in the ''70s
by people like Djikstra, Hoar and Wirth (to name only
a few.) The thing is, if you properly use the formal program
derivation methods they advocated, then you don''t need
it: your program will be as correct as it''s possible to get
short of your being promoted to godhood.

So the conclusion here is that static typing is an attempt
to make programming safe for people that shouldn''t be
programming in the first place. This may sound a bit
cynical, but most real uber-programmers have either
Lisp or Smalltalk in their backgrounds, and
frequently both one. Neither of those languages
have static typing, and they simply don''t need it.

So if the problem is reducing errors, then maybe
we should be working on the places where errors
show up.

Another point that''s sometimes raised is that it''s
useful to provide type information via reflection.
I used to think that was a valid concern until I
started work on PyFit. I had to put a rather
simplistic metadata facility into the program to
substitute for not having type information, and
I found that it was incredibly useful for other
things that you can''t get from reflection on
type data.

John Roth
Bear hugs,
bearophile






John Roth写道:
John Roth wrote:
<关于Artima的评论之一提出了一个相当深刻的问题:静态打字是一个答案。问题是什么?
(这是一个意思。)

每个人似乎都给出的答案是它可以防止错误并澄清程序。


< shrug>它可能只是我,但我认为这是为了简化代码

分析和编译。 (也就是说,对于

一般的静态类型的使用,特别是对于Python的使用。)


看着C,这是值得怀疑的错误预防和程序说明

是静态打字系统中的一个严肃目标。结论C是静态类型的,因为它允许

编译器更容易地为特定的
。 >
变量,并确保正确的添加操作码被放下。


现在这是否对Python有用是一个悬而未决的问题。

当你进入编写测试的学科然后确切地编写测试通过所需的代码时,许多所谓的优点根本就没有了
一键击更多。
....这并不是说TDD是
正确性的全部和最终结果。


对。对于不使用它们的人来说,单元测试不做任何事情。

问题是,如果Guido声明TDD是一种真正的编程方式
$ b Python中的$ b。或者,或者应该在语言设计中做出让步。

那些不喝TDD Kool-aide的人。

因此,这里的结论是,静态打字是一种尝试,使编程安全,对于那些不应该首先编程的人来说。

One of the comments on Artima asks a rather profound
question: static typing is an answer. What''s the question?
(That''s a paraphrase.)

The answer that everyone seems to give is that it
prevents errors and clarifies the program.
<shrug> It might just be me, but I thought it was to simplify code
analysis and compilation. (That is, for the use of static typing in
general, not for Python in particular.)

Looking at C, it''s doubtful error prevention and program clarification
was a serious objective in the static typing system. It''s more
reasonable to conclude that C is statically typed because it allows the
compiler to more easily allocate 1 vs 2 vs 8 bytes for a particular
variable, and to make sure the proper addition opcodes get put down.

Now whether this would be useful for Python is an open question.
Many of the supposed advantages simply aren''t there
when you go to the discipline of writing a test and then
writing exactly the code needed to make the test pass, and
not one keystroke more. .... This isn''t to say TDD is the be-all and end-all of
correctness.
Right. And unit tests don''t do anything for people who don''t use them.
The question is, should Guido state "TDD is the one true way to program
in Python.", or should concessions be made in the language design for
those who don''t "drink the TDD Kool-aide".
So the conclusion here is that static typing is an attempt
to make programming safe for people that shouldn''t be
programming in the first place.




我这样反驳:精英混蛋。 < wink and half>


Python的一个吸引人之处在于它欢迎新手和

所有才能的程序员。你不必是一个超级程序员。

使用它并使用它。我们应该把它蹒跚而不适合那些可怜的程序员吗?

不是。但是,这并不是为什么它不能变得更容易和更安全

用于当它没有损害

电源程序员的实用性时,业余爱好者。 (我的观点)Python不应该在门上有一个标志

说:你必须是这个''leet进入。


静态类型是否可以成为Python的福音?有必要吗?或者是通往哈迪斯的道路上的

小道? <耸肩>只有时间会证明。



I rebut it thusly: "elitist bastard." <wink and a half>

One of the draws of Python is that it''s welcoming to newcomers and
programmers of all talents. You don''t have to be an "uber-programmer" to
use it and use it well. Should we hobble it to suit poor programmers?
No. But that''s no reason why it can''t be made to be easier and safer to
use for the hobbyist when it doesn''t compromise usefulness for the
power-programmer. (My opinion) Python shouldn''t have a sign on the door
saying: "You must be this ''leet to enter."

Will static typing be a boon for Python? Is it necessary? Or is it the
trailhead on the road to Hades? <shrug> Only time will tell.


这篇关于可选的静态打字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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