简体   繁体   English

如何完全禁止python 2.7中的print关键字?

[英]How to completely ban print keyword in python 2.7?

I am introducing the loggers into my project and I would like to ban the print statement usage in it. 我正在将记录器引入我的项目,但我想禁止在其中使用打印语句。 My intent is to force any future developers to use the loggers and to make sure I replaced all print invocations in project. 我的意图是迫使任何未来的开发人员使用记录器,并确保我替换了项目中的所有打印调用。

So far I managed to restrict print('foo') and print 'foo' like invocations with: 到目前为止,我设法通过调用限制了print('foo')print 'foo'调用:

from __future__ import print_function

def print(*args, **kwargs):
    raise SyntaxError("Don't use print! Use logger instead.")

But it is still possible to use print without arguments with intent of adding newline but it won't do anything. 但是仍然可以使用不带参数的print来添加换行符,但它不会做任何事情。

Is it possible to do it without interpreter modifications? 如果不修改解释程序,是否可以做到?

EDIT: I wasn't clear enough I guess from the comments. 编辑:我从评论中还不够清楚。 I just wanted to know if I can prevent the print function for being aliased 我只是想知道我是否可以防止打印功能被混淆

print("foo") # raises exception
print "foo" # doesn't work either
print # doesn't raise any exception, but I want it to
foo = print # this shouldn't work either like the one above, but it does

No, you can't prevent print statements from being used in code that doesn't use from __future__ import print_function . 不,您不能阻止在from __future__ import print_function中不使用的代码中使用print语句。 print statements are not hooked, they are compiled directly to a set of opcodes and the implementation of those opcodes just write directly to stdout or other file object (when using the >> notation). print语句不会被钩住,它们会直接编译为一组操作码 ,而这些操作码的实现只是直接写入stdout或其他文件对象(使用>>表示法时)。

You could go the drastic route of requiring a custom codec , but that's no better than requiring that from __future__ import print_function is used. 您可以采用要求自定义编解码器from __future__ import print_function 途径 ,但这并不比要求使用from __future__ import print_function更好。

By the same token, if all code does use from __future__ import print_function , while you can assign a new print function to the __builtin__ module , you can't prevent someone from building their own version (named print or something else) that writes to sys.stdout , or from executing reload(__builtin__) . 同样,如果所有代码确实都from __future__ import print_function中使用,虽然您可以为__builtin__模块分配新的print功能,但是您不能阻止某人构建自己的版本(命名为print或其他名称)来写入sys.stdout或执行reload(__builtin__) Python is highly dynamic and flexible, I'd not try to lock this down. Python具有高度的动态性和灵活性,我不会尝试将其锁定。

The normal path to enforce coding standards is to use a linter, code review and tests. 实施编码标准的通常途径是使用短绒棉纸,进行代码审查和测试。 You can install hooks on most version control systems that prevent code from being checked in that doesn't pass a linter, and both pylint and flake8 support custom plugins. 您可以在大多数版本控制系统上安装挂钩,以防止未通过pylint情况下检查代码,而pylintflake8支持自定义插件。 You can run a test that configures the logging module to direct all output to a file then raise an exception if anything is written to stdout , etc. 您可以运行将日志记录模块配置为将所有输出定向到文件的测试,然后如果将任何内容写入stdout等,则引发异常。

This is the path that Facebook uses (and it is not alone in this approach), where Python code must pass the Facebook flake8 configuration, which includes the flake8-bugbear extension , and code is autoformatted using Black to make it easy for developers to meet those requirements. 这就是Facebook使用的路径(在这种方法中并不止于此),Python代码必须通过Facebook flake8配置(包括flake8-bugbear扩展名) ,并且代码会使用Black自动格式化,以使开发人员更容易满足这些要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM