最喜欢的非python语言技巧? [英] Favorite non-python language trick?
问题描述
作为第一次学习C的人,当我每次来到Python时,我都会读到关于一个新功能的b $ b,哇!我能做到吗?!切片,dir(),
getattr / setattr,%运算符,所有这些都与C非常不同。
我很好奇 - 什么是非python
语言中每个人最喜欢的技巧?而且 - 为什么不是用Python?
这是我现在的候选人:
所以前几天我是看着语言Lua。在Lua中,你用两个破折号做一个
行评论:
- 嘿,这是评论。
您可以使用 - [[和---]]来阻止评论。
- [[
嘿>
这个
是
a
大
评论
- - ]]
这个语法可以让你做一个漂亮的技巧,你可以添加或减去
第三个破折号来改变代码是否运行: br />
- 这段代码不会运行因为它在评论栏中
- [[
打印(10)
- ]]
- 这个代码会,因为前两个短划线会让其余的一个评论,
打破街区
--- [[
打印(10)
- ]]
所以你可以通过添加
破折号来改变代码是否被注释掉。这比在C或Python中不得不摆脱"更好。或
/ *和* /。当然,IDE可以补偿。但它仍然很整洁:)
As someone who learned C first, when I came to Python everytime I read
about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(),
getattr/setattr, the % operator, all of this was very different from C.
I''m curious -- what is everyone''s favorite trick from a non-python
language? And -- why isn''t it in Python?
Here''s my current candidate:
So the other day I was looking at the language Lua. In Lua, you make a
line a comment with two dashes:
-- hey, this is a comment.
And you can do block comments with --[[ and ---]].
--[[
hey
this
is
a
big
comment
--]]
This syntax lets you do a nifty trick, where you can add or subtract a
third dash to change whether or not code runs:
--This code won''t run because it''s in a comment block
--[[
print(10)
--]]
--This code will, because the first two dashes make the rest a comment,
breaking the block
---[[
print(10)
--]]
So you can change whether or not code is commented out just by adding a
dash. This is much nicer than in C or Python having to get rid of """ or
/* and */. Of course, the IDE can compensate. But it''s still neat :)
推荐答案
" Joseph Garvin" < K0 ***** @ kzoo.edu> ha scritto nel messaggio
新闻:ma ********************************* *** @ pyth on.org ...
"Joseph Garvin" <k0*****@kzoo.edu> ha scritto nel messaggio
news:ma**************************************@pyth on.org...
- 这段代码不会运行,因为它在评论栏中
- [[
打印(10)
- ]]
- 这个代码会,因为前两个破折号使剩下的一个评论,
打破了这个区块
--- [[
print(10)
- ]]
所以你可以通过添加一个
破折号来改变代码是否被注释掉。这比在C或Python中不得不摆脱"更好。或
/ *和* /。当然,IDE可以补偿。但它仍然很整洁:)
--This code won''t run because it''s in a comment block
--[[
print(10)
--]]
--This code will, because the first two dashes make the rest a comment,
breaking the block
---[[
print(10)
--]]
So you can change whether or not code is commented out just by adding a
dash. This is much nicer than in C or Python having to get rid of """ or
/* and */. Of course, the IDE can compensate. But it''s still neat :)
python:
"""
打印10
""
和
#""" ;
打印10
#"""
C ++:
/ *
打印(10);
* /
和
/// *
print(10);
// * /
?
再见,
Enrico
python:
"""
print 10
"""
and
#"""
print 10
#"""
C++:
/*
print(10);
*/
and
///*
print(10);
//*/
?
Bye,
Enrico
2005年6月24日星期五上午09:18 Enrico写道:
>
[...]
Friday 24 June 2005 09:18 am Enrico wrote:
[...]
- 这个代码会,因为前两个破折号会使其余部分成为评论,
打破障碍
--- [[
print(10)
- ]]
--This code will, because the first two dashes make the rest a comment,
breaking the block
---[[
print(10)
--]]
[...]
python:
"""
打印10
"""
和
#""
print 10
#"""
C ++:
/ *
print(10);
* /
和
// / *
print(10);
// * /
?
[...]
python:
"""
print 10
"""
and
#"""
print 10
#"""
C++:
/*
print(10);
*/
and
///*
print(10);
//*/
?
我认为*技巧*这里是如果你有更大的块你只需要
来改变评论的一边,即开头行,取消评论
块而不用搜索结尾并评论出来。
Ciao
Uwe
I think the *trick* here was that if you had larger blocks you''d only have
to change one side of the comment, i.e. the opening line, to de-comment the
block without searching the end of it and commenting that out aswell.
Ciao
Uwe
Joseph Garvin写道:
Joseph Garvin wrote:
我很好奇 - 从非python语言中,每个人最喜欢的技巧是什么?而且 - 为什么不是用Python?
I''m curious -- what is everyone''s favorite trick from a non-python
language? And -- why isn''t it in Python?
Duff'的设备是横向思维的经典杰作。
It由于许多基本原因,Python无法实现,我们不会有风险。
Lorenzo Gatti
Duff''s device is a classic masterpiece of lateral thinking.
It is not possible in Python for many fundamental reasons, we are not
at risk.
Lorenzo Gatti
这篇关于最喜欢的非python语言技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!