python有“严格使用"吗?和“使用警告";像在 perl 中? [英] Does python have a "use strict;" and "use warnings;" like in perl?

查看:70
本文介绍了python有“严格使用"吗?和“使用警告";像在 perl 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 perl 和 python ......同时,这不是我的设计,但必须完成.

I am learning perl and python... at the same time, not my by design but it has to be done.

问题:

在 perl 脚本中,我在 txt 的开头使用(见下文).

In a perl script I use(see below) at the head of my txt.

#!/usr/bin/env perl

use strict;
use warnings;

我应该为我的 Python 脚本做一些日常工作吗?

Is there something I should be doing on routine for my python scripts?

推荐答案

为了提供一个可能避免这里的评论噪音的答案,我将尝试另一个.

To provide an answer that perhaps avoids a little of the commentary noise here, I'll try another one.

原始问题中的两个语用确实扩展为:

The two pragmata in your original question really expand to:

use strict "vars";
use strict "refs";
use strict "subs";
use warnings;

依次回答:

  • use strict "vars" 的效果是导致编译时错误引用一个变量而不先声明它存在(例如在更多静态语言如 C、C++ 和 Java).因为 Python 没有特定的语法来声明变量存在,所以它没有等价物.如果名称不存在,则在 Python 中分配名称总是会创建它.strict 的这个特性没有 Python 等价物,它提供的安全性无法重现.
  • The effect of use strict "vars" is to cause a compile-time error to refer to a variable without first declaring that it exists (such as is the default in more static languages such as C, C++ and Java). Because Python does not have specific syntax to declare that a variable exists, it has no equivalent. Assigning to a name in Python always creates it if it didn't exist first. This feature of strict has no Python equivalent and the safety it provides cannot be recreated.

例如:

$ perl -c -e 'use strict "vars"; $foo = 1'
Global symbol "$foo" requires explicit package name at -e line 1.
-e had compilation errors.

$ perl -c -e 'no strict "vars"; $foo = 1'
-e syntax OK

  • use strict "refs" 的效果是禁止使用包含(现有或新)变量名称的纯字符串作为对变量本身的引用.Python 不会这样做,因此无需禁用它.
    • The effect of use strict "refs" is to disallow the use of plain strings containing the name of an (existing or new) variable as a reference to the variable itself. Python does not do this so has no need to disable it.
    • 例如:

      $ perl -e 'use strict "refs"; ${"message"} = "hello"; print $message'
      Can't use string ("message") as a SCALAR ref while "strict refs" in use at -e line 1.
      
      $ perl -e 'no strict "refs"; ${"message"} = "hello"; print $message'
      hello
      

      • use strict "subs" 的效果是导致编译时尝试调用已知不存在的函数.Python 不执行任何此类检查,也无法启用此类功能.
        • The effect of use strict "subs" is to cause a compile-time any attempt to call a function that is known not to exist. Python does not perform any such checking, and has no way to enable such a feature.
        • 例如:

          $ perl -c -e 'use strict "subs"; foo'
          Bareword "foo" not allowed while "strict subs" in use at -e line 1.
          -e had compilation errors.
          
          $ perl -c -e 'no strict "subs"; foo'
          -e syntax OK
          

          • use warnings 的作用是在编译和运行时启用更多警告,这些行为在早期版本中是默认的,有时可能是需要的,或者从来没有好主意,但严格来说不是错误.例如,使用未初始化的值作为数字通常应该发出警告,但最初并没有这样做.
            • The effect of use warnings is to enable more warnings at both compile- and runtime of various categories of behaviour that was default in earlier versions, may at times be desired, or which has never been a good idea but isn't strictly an error. For example, the use of uninitialised values as numbers ought usually to give a warning, but originally it did not do so.
            • 例如:

              $ perl -e 'use warnings; my $u; print 2 + $u'
              Use of uninitialized value $u in addition (+) at -e line 1.
              2
              
              $ perl -e 'no warnings; my $u; print 2 + $u'
              2
              

              最后;一些评论认为 Python 在 __future__ 中有类似的功能.然而,这不应被视为与 Perl 的 pragmata 类似,因为后者的大部分都是词法范围的,并且可以根据需要在小范围内启用或禁用;Python 的 __future__ 在哪里只对整个源文件启用.

              Finally; some comments have been made that Python has similar functionality in __future__. However, this should not be considered similar to Perl's pragmata, as most of the latter are lexically-scoped, and can be enabled or disabled within small scopes as required; where's Python's __future__ is only enabled for an entire source file.

              例如

              use strict;
              use warnings;
              
              my $total;
              
              $total += count_things($_) for @list;
              
              {
                 no warnings 'uninitialized';
                 printf "The total is %d\n", $total;
              }
              

              一个有点人为的例子,但这个例子演示了使用 no warnings 'uninitialized' 来禁用关于在 printf 语句中使用未初始化值的警告,同时仍然在其他地方启用其他警告.

              A somewhat-contrived example, but this one demonstrates the use of no warnings 'uninitialized' to disable the warning about using an uninitialised value simply within the printf statement, while still keeping the other warnings enabled everywhere else.

              总而言之:Python 没有 use strict 或任何近似的等价物,因为它提供的任何安全功能要么是强制性的,要么在 Python 语言中不可用,并且没有使用警告.它提供的那些功能仅在文件级别启用,不能按范围有选择地启用或禁用.

              In summary then: Python does not have a use strict or any near-equivalent as any of the safety features it provides are either mandatory or not available in the Python language, and does not have a use warnings. Those features it does provide are enabled only at the file-level and cannot be selectively enabled or disabled per scope.

              实际上我现在已经被告知 Python 确实有一些可控的警告标志,可以根据需要启用和禁用.

              Actually I have now been informed that Python does have some controllable warning flags, that can be enabled and disabled as required.

              这篇关于python有“严格使用"吗?和“使用警告";像在 perl 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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