还有一些说明 [英] Some more notes

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

问题描述

Ville Vainio:

Ville Vainio:

新手们建议改进
语言是非常典型的。他们通常会知道他们错了,但随后的讨论可能会很有成果: - )。
It''s highly typical for the newbies to suggest improvements to the
language. They will usually learn that they are wrong, but the
discussion that ensues can be fruitfull anyway :-).




关于语言的几点注释。我不知道我是否真的可以建议

改进语言...但我希望学到一些东西:-)


我想一些Delphi / Pascal中的东西比较好(比如:= for

赋值而不是=和=表示相等,我建议在

我的先例说明中):


1)打印和printf一对没有自动添加空格

参数之间,有或没有自动换行,而不是

当前的打印命令。因为添加了许多+ str()+不是
对我有好处(有sys.stdout.write,但它不是内置的)。

2)添加 case var of:可能很有用。

3)do while (或重复直到)也可以添加,它是有用的。

4)也许真正的原始字符串RR""可以添加以避免在Win上使用

文件路径出现问题: - ]


5)内部函数定义,这样的可变类型的赋值
可以禁止
并被口译员视为错误:

def func(a = [1,2]):

...


6)鉴于:

a = [1]

a + = [2]

a = a + [3 ]

第一个任务扩展了列表,第二个创建了一个新列表

并重新绑定它。为什么这种差异对于看起来相同的操作呢?

不是一种错误。以非常相似的方式做不同的事情

查看命令?


7)有些东西我不明白。我不是OOP纯粹主义者,

但是除了常规方法之外找到这样的函数对我来说似乎很奇怪:

len(s)长度s

min(s)最小项目s

max(s)最大项目s

为了统一,它们可以是:

s .len()

s.min()

s.max()




谢谢你,

熊抱抱,

Bearophile



Few more notes on the language. I don''t know if I can really suggest
improvements to the language... but I hope to learn something :-)

I think some things are better in Delphi/Pascal (like the := for
assignments instead of = and = for equality, that I suggested in the
my precedent notes):

1) The "print" and "printf" pair without automatically added spaces
between arguments, and with or without automatic newline, instead of
the current "print" command. Because adding lots of "+str()+" isn''t
good for me (there''s sys.stdout.write, but it''s not built-in).
2) Adding "case var of:" can be useful.
3) "do while" (or "repeat until") can also be added, it''s useful.
4) Maybe really raw strings RR"" can be added to avoid problems with
file paths on Win :-]

5) Inside function definitions, assignments of mutable types like this
can be forbidden and seen as errors by the interpreter:
def func(a=[1,2]):
...

6) Given:
a = [1]
a += [2]
a = a + [3]
The first assignment extends the list, and second creates a new list
and rebinds it. Why such difference for operations that look the same?
Isn''t a kind of "error" to do different things with quite similar
looking commands?

7) There''s something that I don''t understand. I''m not a OOP purist,
but finding functions like this beside normal methods seems odd to me:
len(s) length of s
min(s) smallest item of s
max(s) largest item of s
For uniformity they can be:
s.len()
s.min()
s.max()
etc.

Thank you,
bear hugs,
Bearophile

推荐答案

bearophile写道:
bearophile wrote:
我不知道我是否真的可以建议改进语言...
I don''t know if I can really suggest improvements to the language...




不,你可以''吨。您的建议是由其他任何东西驱动的

但是您希望使Python类似于Pascal,因为

这是您习惯使用的语言。


Istvan。



No you can''t. Your suggestions are driven by nothing else
but your desire to make Python resemble Pascal since
that is the language that you are accustomed to.

Istvan.


在星期四,2004-10-21 15:16 -0700,bearophile写道:
On Thu, 2004-10-21 at 15:16 -0700, bearophile wrote:
我觉得Delphi / Pascal中有些东西比较好(比如:= for
赋值而不是=和=表示相等,我在
我的先例说明中提到过):


实际上,由于赋值运算符的使用频率远远高于

等于运算符,因此需要三次击键而不仅仅是

一个是个坏主意。

1)打印和printf对,没有自动添加空格
参数之间,有或没有自动换行,而不是当前的打印和命令。因为添加了许多+ str()+对我没有好处(有sys.stdout.write,但它不是内置的)。


使用字符串插值。但是,我同意打印作为声明

而不是函数是一个(非常小的)疣。幸运的是,它很容易被绕过(并且我听说有人说这实际上是在Python 3000中改变了
)。

2)添加case var of:可能很有用。


不熟悉这个结构。如果你的意思是开关/案例如在C(或

的一些变体),那么我绝对同意。

3)do while (或者重复直到)也可以添加,它是有用的。


偶尔有用。不足以把它放在我要用Python看到的东西列表中,但是我可以看到它的实用性*稍微*

更符合逻辑代码(相对于while True:if x:break成语)。

4)也许真正的原始字符串RR""可以添加以避免Win上的文件路径出现问题: - ]


不确定你指的是什么问题因为我很少使用它

平台和我使用它的时代我没有任何问题。你是否b / b $ b表示\的问题?为什么不使用/?

5)在函数定义内部,可以禁止像这样的可变类型的赋值,并被解释器视为错误:
def func(a = [1,2]):


我不明白为什么有效的Python语法应该是一个错误。不可否认,这个

经常会惹恼Python的新用户,但是有很多东西可以让b / b $ b混淆新手,所以我不知道为什么这个特殊的构造应该

单挑出来,特别是当有完全有效的解释

时产生的行为。

6)鉴于:
a = [ 1]
a + = [2]
a = a + [3]
第一个任务扩展了列表,第二个创建了一个新列表
并重新绑定它。为什么看起来相同的操作有这样的差异?
不是一种错误。使用非常相似的命令执行不同的操作?


我同意这可能令人困惑。然而,就像你之前提到的可变论点

一样,有一个合理的解释就足以打破大量的Python代码。

7)有些东西我不明白。我不是OOP纯粹主义者,但是在常规方法旁边找到这样的函数对我来说似乎很奇怪:
len(s)长度s
min(s)最小项目s
max(s)最大项目s
为了统一,它们可以是:
s.len()
s.min()
s.max()
等等。


我没有一个好的答案。至少其中一些可能是纯粹的历史记录(len()在字符串和列表上工作,并且字符串没有用于支持方法的
,所以我'我猜这是一个一致性问题然后

然后它现在是兼容性问题,为什么它不作为一种方法添加

是个好问题) 。至于min()和max()< shrug>。也许为了b $ b避免在这种常用结构上采用过多的方法?在什么

点你停止向类添加新方法?也许决定

就这么简单。

谢谢,
熊拥抱,
Bearophile
I think some things are better in Delphi/Pascal (like the := for
assignments instead of = and = for equality, that I suggested in the
my precedent notes):
Actually, since the assignment operator is used far more often than the
equality operator, making it require three keystroke rather than just
one is a bad idea.
1) The "print" and "printf" pair without automatically added spaces
between arguments, and with or without automatic newline, instead of
the current "print" command. Because adding lots of "+str()+" isn''t
good for me (there''s sys.stdout.write, but it''s not built-in).
Use string interpolation. However, I agree having print as a statement
rather than a function is a (very small) wart. Luckily it''s easily
circumvented (and I''ve heard rumor that this is, in fact, slated to be
changed in Python 3000).
2) Adding "case var of:" can be useful.
Not familiar with this construct. If you mean "switch/case" as in C (or
some variation of same), then I absolutely agree.
3) "do while" (or "repeat until") can also be added, it''s useful.
Occasionally useful. Not enough to put it high on my list of things I''d
like to see in Python, but I can see its utility for making *slightly*
more logical code (versus the "while True: if x: break" idiom).
4) Maybe really raw strings RR"" can be added to avoid problems with
file paths on Win :-]
Not sure what problems you are referring to since I rarely use that
platform and the times I have used it I haven''t had any issues. Do you
mean issues with "\"? Why not just use "/"?
5) Inside function definitions, assignments of mutable types like this
can be forbidden and seen as errors by the interpreter:
def func(a=[1,2]):
I don''t see why valid Python syntax should be an error. Admittedly this
quite often bites new users of Python, but there are plenty of things to
confuse newbies, so I don''t know why this particular construct should be
singled out, especially when there is a perfectly valid explanation of
the resulting behavior.
6) Given:
a = [1]
a += [2]
a = a + [3]
The first assignment extends the list, and second creates a new list
and rebinds it. Why such difference for operations that look the same?
Isn''t a kind of "error" to do different things with quite similar
looking commands?
I''d agree this can be confusing. However, like the mutable arguments
you mention previously, there is a logical explanation that suffices in
place of breaking tons of Python code.
7) There''s something that I don''t understand. I''m not a OOP purist,
but finding functions like this beside normal methods seems odd to me:
len(s) length of s
min(s) smallest item of s
max(s) largest item of s
For uniformity they can be:
s.len()
s.min()
s.max()
etc.
I don''t have a good answer for this. At least some of this is probably
pure history (len() worked on both strings and lists, and strings didn''t
used to support methods, so I''m guessing it was a consistency issue back
then and it''s a compatibility issue now, why it wasn''t added as a method
is a good question). As far as min() and max() <shrug>. Perhaps to
avoid a plethora of methods on such commonly used constructs? At what
point do you stop adding new methods to classes? Perhaps the decision
was as simple as that.
Thank you,
bear hugs,
Bearophile




每次看到它都会让我感到不安。

问候,

Cliff


-

Cliff Wells< cl ************ @ comcast.net>



That disturbs me every time I see it.
Regards,
Cliff

--
Cliff Wells <cl************@comcast.net>


bearophile写道:
bearophile wrote:
我觉得Delphi / Pascal中的一些东西更好


他们可能会更好_in_ Delphi / Pascal。这并不意味着将它们添加到Python中会使Python成为更好的语言。 (我喜欢

大蒜。我喜欢巧克力。我不想吃巧克力覆盖的

大蒜丁香,也不想吃大蒜味的巧克力棒。)

1)打印和printf对,没有自动添加空格
参数之间,有或没有自动换行,而不是当前的打印和命令。因为添加了许多+ str()+不是对我有好处(有sys.stdout.write,但它不是内置的)。



但是Python *确实*已经有字符串插值:print"%s%s%s" %

('''',''''',''三'')


这比printf()更灵活,因为它可以用来

生成字符串,即使没有打印。 C不得不求助于创建一个

sprintf()函数,它执行printf() - 就像格式化而不实际打印
; Python对字符串进行了内置操作。我发现

我一直使用这个,并且从不担心打印语句在逗号之后添加一个

空格,因为我不需要使用逗号。 br />
2)添加case var of:可能很有用。


不确定这是什么,但如果你指的是一个案例/开关

构造:使用一个功能的词典是(IMO)一个更好,更清洁的方式

实现相同的想法。

3)do while (或重复直到)也可以添加,它是有用的。



有点有用,当然,但实现这一目标并不难同样的事情

''而1:如果cond:break''。 Python的设计师做了一个深思熟虑的决定,以最大限度地减少不同类型的循环结构的数量,以便使用
,因为它们往往很容易在两种类型之间进行转换

意味着学习更少。所以我们有两个 - 一个基于条件的

状态循环,一个循环遍历

序列中的每个对象。这些主题的微小变化(根据Python

哲学)通过明确说明规则而不是通过为每个变体创建新语法来更好地完成。

4)也许真正的原始字符串RR""可以添加以避免Win上的文件路径问题: - ]


不是真的,因为Python不知道应该是什么是一个文件

路径,什么不是。当然,除非你明确这个,否则

意味着使用os.path。在这种情况下,您已经使用标准字符串避免了这些问题,并且不需要担心字符串是否为原始字符,而不是担心字符串是否是原始的,更不用说发明一个字符串了。新的额外原始

字符串。

5)在函数定义内部,可以禁止像这样的可变类型的赋值
被视为错误翻译:
def func(a = [1,2]):


除了以这种方式共享可变类型之外可以是一个有用的

功能。它与(在C / Java中)声明一个

局部变量是静态的大致相同。

6)鉴于:
a = [ 1]
a + = [2]
a = a + [3]
第一个任务扩展了列表,第二个创建了一个新列表
并重新绑定它。为什么看起来相同的操作有这样的差异?
不是一种错误。使用非常相似的命令执行不同的操作?


使用+ =的想法是它就地修改。就地

修改对于不可变类型没有意义,但对于可变类型它可能非常有用。具有+ = x平均就地修改

而a = a + x创建新对象允许两个选项在没有函数调用语法的情况下轻松访问。让他们两个都创建一个

新对象意味着必须调用a.extend(x)才能获得原地

修改。而且我猜这绝大多数情况下使用了+ =

,无论如何,就地修改是最好的行为。

7)有我不明白的东西。我不是OOP纯粹主义者,但是在常规方法旁边找到这样的函数对我来说似乎很奇怪:
len(s)长度s
min(s)最小项目s
max(s)最大项目s
为了统一,它们可以是:
s.len()
s.min()
s.max()
等等
I think some things are better in Delphi/Pascal

They may well be better _in_ Delphi/Pascal. That doesn''t mean that
adding them into Python will make Python a better language. (I love
garlic. I love chocolate. I do not want to eat chocolate-covered
garlic cloves, nor garlic-flavored chocolate bars.)
1) The "print" and "printf" pair without automatically added spaces
between arguments, and with or without automatic newline, instead of
the current "print" command. Because adding lots of "+str()+" isn''t
good for me (there''s sys.stdout.write, but it''s not built-in).


But Python *does* already have string interpolation: print "%s%s%s" %
(''one'', ''two'', ''three'')

This is more flexible than printf() would be, because it can be used to
generate strings even without printing. C had to resort to creating a
sprintf() function that did printf()-like formatting without actually
printing; Python made that a built-in operation on strings. I find that
I use this all the time, and never worry about print statements adding a
space after a comma because I don''t need to use commas.
2) Adding "case var of:" can be useful.


Not sure exactly what this is, but if you''re referring to a case/switch
construct: using a dict of functions is (IMO) a better and cleaner way
of implementing the same idea.
3) "do while" (or "repeat until") can also be added, it''s useful.


Somewhat useful, sure, but it''s not hard to achieve the same thing with
''while 1: if cond: break'' . Python''s designer made a deliberate
decision to minimize number of different types of loop structures to
use, since they tend to be very easy to convert between and fewer types
means less to learn. So we have two -- one that loops based on the
status of a condition, and one that iterates over each object in a
sequence. Minor variations on these themes are (according to Python
philosophy) better done by explicitly stating the rules, rather than by
creating new syntax for each variation.
4) Maybe really raw strings RR"" can be added to avoid problems with
file paths on Win :-]


Not really, because Python doesn''t know what''s supposed to be a file
path and what isn''t. Unless, of course, you are explicit about it,
which means using os.path. In which case, you have already avoided
those problems using standard strings and don''t need to worry about
whether a string is raw or not, let alone inventing a new "extra-raw"
string.
5) Inside function definitions, assignments of mutable types like this
can be forbidden and seen as errors by the interpreter:
def func(a=[1,2]):


Except that having mutable types be shared this way can be a useful
feature. It''s approximately the same thing as (in C/Java) declaring a
local variable to be static.
6) Given:
a = [1]
a += [2]
a = a + [3]
The first assignment extends the list, and second creates a new list
and rebinds it. Why such difference for operations that look the same?
Isn''t a kind of "error" to do different things with quite similar
looking commands?


The idea of using += is that it modifies in-place. In-place
modification isn''t meaningful for immutable types, but it can be very
useful for mutable types. Having a += x mean in-place modification
while a = a + x creates a new object allows both options to be easily
accessible without function-call syntax. Having both of them create a
new object means that one must call a.extend(x) in order to get in-place
modification. And I''d guess that the vast majority of cases where += is
used, in-place modification is the preferable behavior anyhow.
7) There''s something that I don''t understand. I''m not a OOP purist,
but finding functions like this beside normal methods seems odd to me:
len(s) length of s
min(s) smallest item of s
max(s) largest item of s
For uniformity they can be:
s.len()
s.min()
s.max()
etc.




你显然更像是一个OOP纯粹主义者而不是Python ...拥有这些

因为函数而不是方法对Python的动态

类型系统来说要简单得多。例如,新的序列类型不需要定义min()

和max()方法 - 现有的内置函数正常工作。它还意味着可以改进函数的实现细节,而不需要重写每个已定义len()方法的类,
$

b $ b并且Python可以在函数

本身和一个或多个辅助方法之间拆分函数的工作,这些方法可能(或可能不是)在任何方面实现
给定课程。


请记住,良好的语言设计不仅仅是噢,

这篇文章可能有用!你还必须考虑所有

块如何组合在一起,以及某些星座如何与其他星座相互作用。当连接到正确的
车辆时,推土机铲刀,割草机和

粪便撒播机都是非常有用的设备,但你不会想要它们在你的通勤车上,对?坚持使用有用设备的一小部分更好地支持
。这是一致的(最重要的)实用方式。


Jeff Shannon

技术员/程序员
Credit International



You''re apparently more of an OOP purist than Python is... Having these
as functions instead of methods is much simpler for Python''s dynamic
type system. For example, new sequence types don''t need to define min()
and max() methods -- the existing builtins just work. It also means
that implementation details of the function can be improved without
requiring a rewrite of every class that has ever defined a len() method,
and that Python can split the work of the function between the function
itself and one or more helper methods that might (or might not) be
implemented on any given class.

Remember, there''s a lot more to good language design than just "Ooh,
this piece might be useful!" You also have to consider how all the
pieces fit together, and how certain constellations of pieces interact
with other constellations of pieces. Bulldozer blades, furrowers, and
manure-spreaders are all very useful devices when attached to the right
vehicle, but you wouldn''t want them on your commuter car, right? Much
better to stick with a small subset of "useful devices" which fit
together in a consistent and (most importantly) practical way.

Jeff Shannon
Technician/Programmer
Credit International


这篇关于还有一些说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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