几个在python runtime里的小技巧

有时候要在runtime里进行调试,例如测试下某个变量的值,我就问了chatgpt

me:how run python file in python run time

chatgpt:To run a Python file within another Python script at runtime, you can use the execfile() function, which allows you to execute a file as if it were a Python script. However, please note that the execfile() function is no longer available in Python 3.x.

Here's an example of how you can use execfile() in Python 2.x:

execfile('path/to/file.py')

If you are using Python 3.x, you can achieve a similar result using the exec() function. Here's an example:


with open('path/to/file.py', 'r') as file:
    code = file.read()

exec(code)

In the above examples, replace 'path/to/file.py' with the actual path to the Python file you want to run. The file will be executed at runtime, and its code will be executed within the context of the running script.

Keep in mind that using execfile() or exec() to execute arbitrary code from external files can be a security risk, especially if the code comes from untrusted sources. Exercise caution when executing external code in this manner.

下面就是我在VS的终端界面里,执行的情况,很不错.

>>> with open('mnist_show.py', 'r') as file:
...     code = file.read()
... 
>>> exec(code)

我又问了chatgpt一个愚蠢的问题,使用了非常幼稚的英文

ChatGPT竟然给出了非常好的答案。

最近网上在讨论是否大模型会污染我们现实语言的环境,因为大量生成的预料会被用来进行训练,类似电路里“自激”的现象,同时有人在知乎的旅游问答专栏下,发现了大量明显是chatgpt给出的错误答案。进而引发了是不是chatgpt正在严重污染“简中”环境的问题。

我倒不担心,一个垃圾环境如何被污染的更脏呢?

反而我担心,chatgpt这么强大,人类在他面前就像一个小孩子,chatgpt就像一个至尊的王者,面对所有人类的错误、幼稚都采取宽容的态度,就像我刚刚使用的笨拙英文,他不纠正我,而是给出了正确答案。

这就好比给了人类大脑一个拐杖,大脑就开始了萎缩。

Leave a Reply

Your email address will not be published. Required fields are marked *