Python Feature Request:显式变量声明 [英] Python Feature Request: Explicit variable declarations

查看:86
本文介绍了Python Feature Request:显式变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。请告诉我这个功能请求对于python是否理智(而不是之前为b
),因此可以将其发布到python-dev邮件

列表中。首先我要说的是,我不是一个专业的程序员,而且你的技术知识太多了。


我希望有类似明确选项的东西。在

Visual Basic中的声明,它打开类似于C的检查来声明

变量。这对于那些来自C / C +
的专业人士,因为这有助于防止拼写错误喜欢:


sunlognitude = sunlongitude + 180.0

用户无意中输入了g在n之前并且稍后会想知道为什么他的申请没有按预期运作。

解决方案

4月14日,上午11:21,samj ... @ gmail.com写道:


您好。请告诉我这个功能请求对于python是否理智(而不是之前为b
),因此可以将其发布到python-dev邮件

列表中。首先我要说的是,我不是一个专业的程序员,而且你的技术知识太多了。


我希望有类似明确选项的东西。在

Visual Basic中的声明,它打开类似于C的检查来声明

变量。这对于那些来自C / C +
的专业人士,因为这有助于防止拼写错误喜欢:


sunlognitude = sunlongitude + 180.0

用户无意中输入了g在n之前并且稍后会想知道为什么他的申请没有按预期工作。



请阅读:
http://www.python.org/doc/faq/progra ... tatic-analysis


PyChecker和Pylint可能会帮到你。


- Paddy。


sa*****@gmail.com 写道:
< blockquote class =post_quotes>
您好。请告诉我这个功能请求对于python是否理智(而不是之前为b
),因此可以将其发布到python-dev邮件

列表中。首先我要说的是,我不是一个专业的程序员,而且你的技术知识太多了。


我希望有类似明确选项的东西。在

Visual Basic中的声明,它打开类似于C的检查来声明

变量。这对于那些来自C / C +
的专业人士,因为这有助于防止拼写错误喜欢:


sunlognitude = sunlongitude + 180.0

用户无意中输入了g在n之前并且稍后会想知道为什么他的申请没有按预期工作。



我没有正式的编程培训,因此不得不动摇关闭

所有最糟糕的习惯都会影响到业余爱好者,这是我现在享受的适度的b / b
熟练程度。所以,请允许我向你保证,这是我可以说老实说的只是你最不担心的事情。

因此没有地方堵塞像python这样的语言。


但是,如果你有自虐倾向并希望在你的编程中花费大量的开销,你总是可以无情地将自己绑在课堂上:

class C(object):

声明= [''bob'',''carol'','ted'',''alice'']

def __setattr __(self,anattr,aval):

如果解析不在C.declared中:

引发TypeError,就是不能把你搞定,兄弟。

其他:

self .__ dict __ [anattr] = aval

例如:


pyclass C(对象):

....声明= [''bob'',''carol'','ted'',''alice'']

.... def __setattr __(self,anattr,aval):

....如果不是C.declared中的anattr:

... 。引发TypeError ,只是不能挂你,兄弟。

....其他:

.... self .__ dict __ [anattr] = aval

....

pyc = C()

pyc.bob = 42

pyc.bob



pyc.x = 69

回溯(最近一次调用最后一次):

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

文件"< stdin>",第5行,__ setattr__

TypeError:只能''请你把你搞定,兄弟。

将这种丑陋扩展到类型检查留给了

读者的练习。使用元类来进行这样的废话是为了
metaclass maniacs的练习。


James


sa*****@gmail.com schrieb:

$ b $你好请告诉我这个功能请求对于python是否理智(而不是之前为b
),因此可以将其发布到python-dev邮件

列表中。首先我要说的是,我不是一个专业的程序员,而且技术知识太多了。



我相信它之前已被请求,但我现在懒得搜索

进行事先讨论。请求是理智的,但也是不完整的:

没有为局部变量的实际声明建议语法,

并且没有讨论这是否适用于本地变量

,或者也适用于全局变量和对象属性。


我认为这样的请求的可能性相当低

考虑。这是一个很大的变化(明确的实施,并且文件明智地为b $ b b),并且可能存在争议。因此,首先要写一个PEP(参见PEP 1了解详情)。


问候,

Martin


Hello. Please tell me whether this feature request is sane (and not
done before) for python so it can be posted to the python-dev mailing
list. I should say first that I am not a professional programmer with
too much technical knowledge.

I would like to have something like the "option explicit" statement in
Visual Basic which turns on C-like checking for declaration of
variables. This is highly helpful for people who are coming from C/C+
+, for people who are learning programming using Python, and even
professionals, since this helps prevent typo errors like:

sunlognitude = sunlongitude + 180.0

where the user has inadvertantly typed "g" before "n" and will later
wonder why his application is not working as expected.

解决方案

On Apr 14, 11:21 am, samj...@gmail.com wrote:

Hello. Please tell me whether this feature request is sane (and not
done before) for python so it can be posted to the python-dev mailing
list. I should say first that I am not a professional programmer with
too much technical knowledge.

I would like to have something like the "option explicit" statement in
Visual Basic which turns on C-like checking for declaration of
variables. This is highly helpful for people who are coming from C/C+
+, for people who are learning programming using Python, and even
professionals, since this helps prevent typo errors like:

sunlognitude = sunlongitude + 180.0

where the user has inadvertantly typed "g" before "n" and will later
wonder why his application is not working as expected.

Please read this:
http://www.python.org/doc/faq/progra...tatic-analysis

PyChecker and Pylint might help you.

- Paddy.


sa*****@gmail.com wrote:

Hello. Please tell me whether this feature request is sane (and not
done before) for python so it can be posted to the python-dev mailing
list. I should say first that I am not a professional programmer with
too much technical knowledge.

I would like to have something like the "option explicit" statement in
Visual Basic which turns on C-like checking for declaration of
variables. This is highly helpful for people who are coming from C/C+
+, for people who are learning programming using Python, and even
professionals, since this helps prevent typo errors like:

sunlognitude = sunlongitude + 180.0

where the user has inadvertantly typed "g" before "n" and will later
wonder why his application is not working as expected.

I have no formal training in programming and thus have had to shake off
all of the worst habits afflicting amateurs on my path to the modest
proficiency I enjoy now. So let me assure you that this is something I
can honestly say is just about the least of your worries and
consequently has no place clogging up a language like python.

But, if you have masochistic tendencies and want a lot of overhead in
your programming, you can always bind yourself mercilessly to classes:
class C(object):
declared = [''bob'', ''carol'', ''ted'', ''alice'']
def __setattr__(self, anattr, aval):
if anattr not in C.declared:
raise TypeError, "Just can''t hook you up, bro."
else:
self.__dict__[anattr] = aval
E.g.:

pyclass C(object):
.... declared = [''bob'', ''carol'', ''ted'', ''alice'']
.... def __setattr__(self, anattr, aval):
.... if anattr not in C.declared:
.... raise TypeError, "Just can''t hook you up, bro."
.... else:
.... self.__dict__[anattr] = aval
....
pyc = C()
pyc.bob = 42
pyc.bob
42
pyc.x = 69
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __setattr__
TypeError: Just can''t hook you up, bro.
Extending this ugliness to type-checking is left as an exercise for the
reader. Using metaclasses for such nonsense is left as an exercise for
metaclass maniacs.

James


sa*****@gmail.com schrieb:

Hello. Please tell me whether this feature request is sane (and not
done before) for python so it can be posted to the python-dev mailing
list. I should say first that I am not a professional programmer with
too much technical knowledge.

I believe it has been requested before, but I''m too lazy now to search
for prior discussion. The request is sane, but is also incomplete: there
is no syntax suggested for the actual declarations of local variables,
and no discussion whether this is meant to apply to local variables
only, or also to global variables and object attributes.

I think there is a fairly low chance that such a request will be
considered. It''s a big change (implementation-wise, and
documentation-wise), and likely controversial. So one would have
to write a PEP first (see PEP 1 for details).

Regards,
Martin


这篇关于Python Feature Request:显式变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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