跨语言的语法 [英] Syntax across languages

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

问题描述

这篇文章来自一个无聊的早晨,如果你很忙,请忽略这个。

这篇文章仅供轻松的人使用。


我已经发现这个页面,Syntax Across Languages,它包含许多

错误和遗漏,但它很有趣。
http://merd.sourceforge.net/pixel/la...languages.html


与其他语言相比,Python相当不错,快速扫描

扫描只有少数东西在其他语言中看起来更好(通常全部/大多数

所有语言都可以使用,所以它通常只需要简单,优雅等等。


- 类似于Pascal的评论(有用):( * ... *)


- 有关当前行和文件的信息为Ruby:

__LINE__ __FILE __

而不是python版本:

inspect.stack()[0] [2] inspect.stack()[0] [1]

- 〜==近似FP平等


- 比较返回4个值(即低级,平等,高级或不具有可比性),如同Pliant:

比较


- 身份功能: "同一性QUOT;就像在Common Lisp中一样(可能在Python中使用很少的

)。


- 异常重试:在捕获异常后,告诉代码片段

重新运行

" retry"如在Ruby中


- 对象克隆:obj.copy()obj.deepcopy()


- 访问父方法:

super在Ruby中,而不是在Python中:

super(Class,self).meth(args)


- 递归flatten ;就像在Ruby中一样(有用)


也许我的一些错误也有,我有很多东西我不知道abyt Python。


再见,

熊宝宝

This post comes from a boring morning, if you are busy ignore this.
This post is only for relaxed people.

I''ve found this page, "Syntax Across Languages", it contains many
errors and omissions, but it''s interesting.
http://merd.sourceforge.net/pixel/la...languages.html

Compared to the other languages Python comes out rather well, in quick
scan only few things look better in other languages (usually all/most
things are possible in all languages, so it''s often just a matter of
brevity, elegance, etc):

- Nestable Pascal-like comments (useful): (* ... *)

- Information about the current line and file as Ruby:
__LINE__ __FILE__
Instead of the python version:
inspect.stack()[0][2] inspect.stack()[0][1]

- ~== for approximate FP equality

- comparison returns 4 values (i.e. inferior, equal, superior or not
comparable), as in Pliant:
"compare"

- identity function: "identity" as in Common Lisp (probably of little
use in Python).

- Exception retrying: after catching an exception, tell the snippet to
be re-run
"retry" as in Ruby

- object cloning: obj.copy() obj.deepcopy()

- accessing parent method:
super as in Ruby, instead as in Python:
super(Class, self).meth(args)

- recursive "flatten" as in Ruby (useful)

Probably there are some errors of mine too, there are many things I
don''t know aboyt Python yet.

Bye,
bearophile

推荐答案

是************ @ lycos.com 写道:
be************@lycos.com wrote:
- 关于当前行和文件的信息为Ruby:
__LINE__ __FILE__
而不是python版本:
inspect.stack()[0] [2] inspect.stack()[0] [1]


(那个(主要是)CPython依赖的,应该避免)

- 〜 ==近似FP等式


str(a)== str(b)

- 比较返回4个值(即低于,等于,高于或等于
可比较),如Pliant:比较
- Information about the current line and file as Ruby:
__LINE__ __FILE__
Instead of the python version:
inspect.stack()[0][2] inspect.stack()[0][1]
(that''s (mostly) CPython-dependent, and should be avoided)
- ~== for approximate FP equality
str(a) == str(b)
- comparison returns 4 values (i.e. inferior, equal, superior or not
comparable), as in Pliant: "compare"
cmp(" a"," b")
-1 cmp(" a"," a")
0 cmp(" b"," a")
1 cmp("?",u"?")
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

UnicodeDecodeError:''ascii''编解码器无法解码字节/.../


肯定看起来像四种可能的结果。

- 身份功能:身份就像在Common Lisp中一样(可能在Python中很少使用)。


id(a)

- 异常重试:在捕获异常后,告诉代码段重新运行重试如在Ruby
x = 0
而1:
....尝试:

.... x + = 1

....如果x< = 5:

....提高ValueError

....除了ValueError:

.. ..打印重试

....继续

....其他:

.... break

....

重试

重试

重试

重试

重试
cmp("a", "b") -1 cmp("a", "a") 0 cmp("b", "a") 1 cmp("?", u"?") Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: ''ascii'' codec can''t decode byte /.../

sure looks like four possible outcomes.
- identity function: "identity" as in Common Lisp (probably of little
use in Python).
id(a)
- Exception retrying: after catching an exception, tell the snippet to
be re-run "retry" as in Ruby x = 0
while 1: .... try:
.... x += 1
.... if x <= 5:
.... raise ValueError
.... except ValueError:
.... print "retry"
.... continue
.... else:
.... break
....
retry
retry
retry
retry
retry



- 对象克隆:obj.copy()obj.deepcopy()


导入副本


(克隆通常是python中设计问题的一个标志。如果你认为你需要它,你可能不会。)你真的认为你需要它,导入

副本。)

- 递归展平如在Ruby(有用)


- object cloning: obj.copy() obj.deepcopy()
import copy

(cloning is usually a sign of a design problem in python. if you think
you need it, you probably don''t. if you really think you need it, import
copy.)
- recursive "flatten" as in Ruby (useful)




如果你可以定义语义,那就是几行代码。如果你没有确定语义,内置的东西不会帮助你...





< / F>



if you can define the semantics, it''s a few lines of code. if you''re not
sure about the semantics, a built-in won''t help you...

etc.

</F>


感谢您的评论,Fredrik Lundh。
Thank you for the comments, Fredrik Lundh.
(那些(主要)依赖于CPython,应该避免)<


然后,非CPython依赖的方式可能会更有用。


肯定看起来像四种可能的结果。<


正确(但对我来说,四个明确的答案似乎比三个答案更好

还有一个例外)。


id(a)<


我认为在Python中它可能更像(但它有点使用
):

def identity(x):return x

或:

identity = lambda x:x


(克隆通常是python中的设计问题。如果你觉得你需要
,你可能不会。如果你真的认为你需要它,

import

copy 。)<


我同意(导入模块比使用标准复制方法更糟糕,

但我认为这可以看作是一种方式不鼓励在
Python中使用副本。


如果你能定义语义,那就是几行代码。如果你不是
(that''s (mostly) CPython-dependent, and should be avoided)<
Then a non CPython-dependent way of doing it can be even more useful.

sure looks like four possible outcomes.<
Right (but to me four explicit answers seem better than three answers
and an exception still).

id(a)<
I think in Python it can be something more like (but it''s of little
use):
def identity(x): return x
Or:
identity = lambda x: x

(cloning is usually a sign of a design problem in python. if you think you need it, you probably don''t. if you really think you need it,
import
copy.)<

I agree (importing a module is worse than using a standard copy method,
but I think this can be seen as a way to discourage the use of copy in
Python).

if you can define the semantics, it''s a few lines of code. if you''re not



确定语义,内置的东西不会帮助你...<


我认为该语言需要快速内置版本。如果有什么东西

都在Mathematica和Ruby里面,那么它可能也很有用。

Python也是如此:-)


再见并且谢谢你,

bearophile


sure about the semantics, a built-in won''t help you...<

I think the language needs a fast built-in version of it. If something
is both inside Mathematica and Ruby, then probably it can be useful in
Python too :-)

Bye and thank you,
bearophile


是************ @ lycos.com 写道:
be************@lycos.com wrote:
如果你可以定义语义,那就是几行代码。如果你不是
if you can define the semantics, it''s a few lines of code. if you''re not


确定语义,内置的东西不会帮助你...<

我认为语言需要快速构建 - 它的版本。如果在Mathematica和Ruby中都有什么东西
,那么它也可能在Python中很有用: - )


sure about the semantics, a built-in won''t help you...<

I think the language needs a fast built-in version of it. If something
is both inside Mathematica and Ruby, then probably it can be useful in
Python too :-)




numpy已经有一个:

http:// numeric.scipy.org/numpydoc/nu...ml#pgfId-36512


(它可能也在scipy中,但scipy docs don'似乎是免费提供的。
免费提供。嗯...)

PIL也有一个:

http://www.effbot.org/imagingbook/im ... getdata-method


Tkinter中还有一个:



numpy already has one:

http://numeric.scipy.org/numpydoc/nu...ml#pgfId-36512

(it''s probably there in scipy too, but the scipy docs don''t appear to be
freely available. hmm...)

PIL also has one:

http://www.effbot.org/imagingbook/im...getdata-method

there''s also one in Tkinter:

import Tkinter
Tkinter._flatten([" abc",1,2,(3,4,5),None,[6,7,8,(9,)]])
import Tkinter
Tkinter._flatten(["abc", 1, 2, (3, 4, 5), None, [6, 7, 8, (9,)]])



(''abc ',1,2,3,4,5,6,7,8,9)


创建一个通用版本,你必须决定哪些序列要

对待类似序列,以及你不想对待的序列

如序列...


< / F>


(''abc'', 1, 2, 3, 4, 5, 6, 7, 8, 9)

to create a generic version, you have to decide which sequences to
treat like sequences, and which sequences you don''t want to treat
like sequences...

</F>


这篇关于跨语言的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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