“不需要变量或参数声明。” [英] "no variable or argument declarations are necessary."

查看:85
本文介绍了“不需要变量或参数声明。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑进入Python,这是由工程师使用的,我很欣赏 - 谷歌和Bram Cohen,但是很惊讶地读到了


"没有变量或参数声明是必要的。


当然这意味着如果我拼错一个变量名,我的程序将会错误地使用
无法使用错误消息。


如果你没有声明变量,你可能会无意中重复使用封闭上下文中使用的

变量当打算引用现有变量时,无意中引用或b / b
无意中引用了一个新变量(一个错字)。


可以做些什么来迅速检测到这种类型的错误?

-
http://www.jim.com

I am contemplating getting into Python, which is used by engineers I
admire - google and Bram Cohen, but was horrified to read

"no variable or argument declarations are necessary."

Surely that means that if I misspell a variable name, my program will
mysteriously fail to work with no error message.

If you don''t declare variables, you can inadvertently re-use an
variable used in an enclosing context when you don''t intend to, or
inadvertently reference a new variable (a typo) when you intended to
reference an existing variable.

What can one do to swiftly detect this type of bug?
--
http://www.jim.com

推荐答案

James A. Donald写道:
James A. Donald wrote:
我正在考虑进入Python,这就是我们由工程师编辑我很欣赏 - 谷歌和布拉姆科恩,但读起来感到震惊

没有变量或参数声明是必要的。

肯定是这样的意味着如果我拼错变量名称,我的程序将神秘地无法正常工作而没有错误消息。

如果你没有声明变量,你可能会无意中重复使用<当你打算引用现有变量时,当你不打算在一个封闭的上下文中使用变量,或者无意中引用一个新的变量(一个拼写错误)。
<有什么可以迅速检测到这种类型的bug?
I am contemplating getting into Python, which is used by engineers I
admire - google and Bram Cohen, but was horrified to read

"no variable or argument declarations are necessary."

Surely that means that if I misspell a variable name, my program will
mysteriously fail to work with no error message.

If you don''t declare variables, you can inadvertently re-use an
variable used in an enclosing context when you don''t intend to, or
inadvertently reference a new variable (a typo) when you intended to
reference an existing variable.

What can one do to swiftly detect this type of bug?




它是python的基本部分,以及许多其他脚本

语言。

如果你不熟悉它,你可能会尝试一种语言,强制你首先像java或C ++一样声明每个变量。

否则,在python中,我建议使用你可以使用的变量名
轻松拼写。还要对代码进行大量测试。对我来说,这对我来说从来都不是一个问题,尽管如果python是不区分大小写的话会更好,但这种情况永远不会发生。



It''s a fundamental part of python, as well as many other scripting
languages.
If you''re not comfortable with it, you might try a language that forces
you to declare every variable first like java or C++.
Otherwise, in python, I''d recommend using variable names that you can
easily spell. Also do plenty of testing of your code. It''s never been
an issue for me, although it would be nicer if python were
case-insensitive, but that is never going to happen.


James A. Donald写道:
James A. Donald wrote:
我正在考虑进入Python,这是工程师使用的我很欣赏 - 谷歌和Bram Cohen,但是感到害怕阅读

没有变量或参数声明是必要的。

当然这意味着如果我拼错变量名,我的程序将
神秘地无法正常工作,没有错误信息。

如果你没有声明变量,你可能会无意中重复使用封闭环境中使用的
变量当你打算引用一个现有的变量时,打算或者无意中引用一个新变量(一个错字)。

有什么可以快速检测到这种类型的bug?
I am contemplating getting into Python, which is used by engineers I
admire - google and Bram Cohen, but was horrified to read

"no variable or argument declarations are necessary."

Surely that means that if I misspell a variable name, my program will
mysteriously fail to work with no error message.

If you don''t declare variables, you can inadvertently re-use an
variable used in an enclosing context when you don''t intend to, or
inadvertently reference a new variable (a typo) when you intended to
reference an existing variable.

What can one do to swiftly detect this type of bug?




必须在它之前分配一个变量使用,否则引发一个
NameError异常..



A variable has to be assigned to before it is used, otherwise a
NameError exception is thrown..

a + 1
Traceback(最近一次调用最后一次):

文件"<交互式输入>",第1行,在?

NameError:name''a ''未定义a = 1
a + 1
a + 1 Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name ''a'' is not defined a = 1
a + 1



2


错别字变量名很容易被发现,除非在当前语境中存在错误的



将McGugan

-
http://www.willmcgugan.com

" ; .join({''*'':''''',''''''''。''}。get(c,0)或chr(97+(ord(c)-84 )%26)for c in

" jvyy * jvyyzpthtna ^ pbz")


2

Typos in variable names are easily discovered unless the typo happens to
exist in the current context.

Will McGugan
--
http://www.willmcgugan.com
"".join({''*'':''@'',''^'':''.''}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")


哇,从来没有发生过我有人会有一个这个问题!


但是,这个m帮助:

http://www.logilab.org / projects / pylint


更详细:
Wow, never even occured ot me someone would have a problem with this!

But, this might help:

http://www.logilab.org/projects/pylint

In more detail:
当然这意味着如果我拼错变量名,我的程序将会
神秘地无法使用没有错误消息。


不,错误信息实际上会非常清楚:)你是

试图使用一个不存在的变量!这将是从编译语言中获得的相同类型的消息,只是在不同的时间点(运行时与编译时)之间。(br / b) >
如果你没有声明变量,你可能会无意中重复使用在你不打算的封闭上下文中使用的变量,


可能,虽然好的设计应该始终保持这种情况在

bay。 Python是OO,因此范围应该很少是一个问题...全局

主要是邪恶的,所以在任何给定时间的上下文应该是方法,

你'' d需要一个相当大而复杂的方法来开始失去跟踪你所谓的什么...另外,一个好的命名约定应该保持这个

。 />

另外,因为事情被解释了,所以你通常不需要像编译语言一样将

广泛用于事物。你可以按需要快速运行一些东西,一个名不见经传的变量将明确显示,并且可以在几分钟内轻松解决。


使用智能IDE也可以帮助防止这些问题发生吗?


希望你喜欢python :)


JF


詹姆斯·唐纳德写道:我正在考虑进入Python,这是工程师使用的我很欣赏 - 谷歌和布拉姆科恩,但是感到惊恐的阅读

没有变量或参数声明是必要的。

肯定这意味着如果我拼错变量名,我的程序将神秘地无法使用,没有错误消息。

如果您没有声明变量,当您不打算或无意中无意中重复使用封闭上下文中使用的
变量时当您打算引用现有变量时,引用一个新变量(一个拼写错误)。

什么可以我们可以迅速检测到这种类型的错误吗?

-
http://www.jim.com
Surely that means that if I misspell a variable name, my program will
mysteriously fail to work with no error message.
No, the error message will be pretty clear actually :) You are
attempting to use a variable that doesn''t exist! This would be the same
type of message you would get from a compiled language, just at a
different point in time (runtime vs. compile time).
If you don''t declare variables, you can inadvertently re-use an
variable used in an enclosing context when you don''t intend to,
Possible, though good design should always keep anysuch situation at
bay. Python is OO, hence scoping should rarely be a problem ... globals
are mostly evil, so the context at any given time should be the method,
you''d need a fairly big and complex method to start loosing track of
what you called what ... Also, a good naming convention should keep this
at bay.

Also, because things are interpreted, you don''t (normally) need to put
extensive forthought into things as you do with compiled languages. You
can run things quickyl and easily on demand, a misnamed variable will be
clearly indicated and easily solved in a matter of minutes.

Using a smart IDE might also help prevent such problems before they occur?

Hope you enjoy python :)

J.F.

James A. Donald wrote: I am contemplating getting into Python, which is used by engineers I
admire - google and Bram Cohen, but was horrified to read

"no variable or argument declarations are necessary."

Surely that means that if I misspell a variable name, my program will
mysteriously fail to work with no error message.

If you don''t declare variables, you can inadvertently re-use an
variable used in an enclosing context when you don''t intend to, or
inadvertently reference a new variable (a typo) when you intended to
reference an existing variable.

What can one do to swiftly detect this type of bug?
--
http://www.jim.com



这篇关于“不需要变量或参数声明。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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