普通人还有必要学习 Python 之类的编程语言吗?

知乎热榜2个月前发布 NIUC!
148 0 0

惰惰猴的回答

一、没有必要

个人理解的编程,只是一种工具技能。

普通人还有必要学习 Python 之类的编程语言吗?

普通人如果对计算机不感兴趣,或者日常生活、工作中,没有相关使用场景。是没有学习必要的

二、Text 生成式 AI 之自然语言编程

针对问主的补充问题 —— Text 生成式 AI 能用自然语言编程。

普通人还有必要学习 Python 之类的编程语言吗?

再学习 Python 编程的意义如下:

Prompt、Prompt、Prompt

现阶段,不管媒体如何吹 GPT 大模型,它的实质还是根据 Prompt 上下文的 Text 生成。

若不懂 Python,又如何能为 Text 生成模型,提供精确 Prompt 呢?

三、Text 生成式 AI 模型的 Prompt 精确度

比如,我想用 Python 写一个在 MacBook 上查看 IP 地址的图形化 APP(只要运行这个 APP,点一下按钮,就能获取这台 MacBook 的 IP 地址)

普通人还有必要学习 Python 之类的编程语言吗?
点击 button 前
普通人还有必要学习 Python 之类的编程语言吗?
点击 button 后

1、编码前提

我并不是科班的程序员,断断续续使用 Python 的时间也不多。

所以,我选择了自己相对了解的标准库 Tkinter,虽然我很久没用过它了,但我还有一些关于 ButtonCanvas 控件的模糊记忆。

初步想法,就是把 MacOS 终端命令 ifconfig en0inet 信息(inet 后的 IP 地址 字符串), 解析到 Canvas 上。

2、Prompt 调整

这是我对 ChatGPTPrompt 描述:

普通人还有必要学习 Python 之类的编程语言吗?

如下,就是 ChatGPT 根据我的首个 Prompt,返回的代码:

from tkinter import Tk, Canvas, Button
import socket

class My_GUI:
    def __init__(self):
        self.root = Tk()
        self.root.geometry('800x650+200+200')
        self.root.config(bg='skyblue')
        self.root.title('Welcome to Tkinter program')
        
        self.canvas = Canvas(self.root, bg='white', width=600, height=400)
        self.canvas.pack()
        
        self.btn_show_ip = Button(self.root, text='Show IP', command=self.show_ip)
        self.btn_show_ip.pack(pady=10)
        
        self.root.mainloop()
    
    def show_ip(self):
        ip = socket.gethostbyname(socket.gethostname()) # 错误
        self.canvas.create_text(300, 200, text="Your IP Address is: " + ip, font=("Helvetica", 16), fill="black")

# 测试
gui = My_GUI()

就这么简单的需求,而且我还给出了自己的代码。结果还是出现了 Prompt 偏差 ——

ip = socket.gethostbyname(socket.gethostname()) 这条语句,明显是针对 Windows 的。

换句话说,这条语句对于 MacOS 来说,就是无效的。

还好我熟悉 MacOS !

3、AI 不万能

那我直接告诉 ChatGPT ,操作系统是苹果环境好了。

普通人还有必要学习 Python 之类的编程语言吗?

好吧,告诉 ChatGPT 操作系统环境后 ,其给出的代码如下:

from tkinter import Tk, Canvas, Button
import subprocess

class My_GUI:
    def __init__(self):
        self.root = Tk()
        self.root.geometry('800x650+200+200')
        self.root.config(bg='skyblue')
        self.root.title('Welcome to Tkinter program')
        
        self.canvas = Canvas(self.root, bg='white', width=600, height=400)
        self.canvas.pack()
        
        self.btn_show_ip = Button(self.root, text='Show IP', command=self.show_ip)
        self.btn_show_ip.pack(pady=10)
        
        self.root.mainloop()
    
    def show_ip(self):
        ip_output = subprocess.check_output(['ifconfig']) # 明显错误
        ip_output = ip_output.decode('utf-8')
        ip_start_index = ip_output.find('inet ') + len('inet ')
        ip_end_index = ip_output.find(' ', ip_start_index)
        ip = ip_output[ip_start_index:ip_end_index]
        self.canvas.create_text(300, 200, text="Your IP Address is: " + ip, font=("Helvetica", 16), fill="black")

# 测试
gui = My_GUI()

只要仔细看,其生成的新代码,还是会闹笑话

毕竟 MacOS 终端 ifconfig 的首部分回显,永远都是环回口 loopback0 。因此,上述代码永远都只会匹配出下图 lo0 网卡 的环回口地址 127.0.0.1

普通人还有必要学习 Python 之类的编程语言吗?

行文至此,可以看出,即便是一个简单的需求,也要依赖精确的 Prompt !!!

3、了解 Python 和 MacOS,是完成该需求的前提

如果 AI 使用者,了解 Python 和 MacOS 的话,立马就能看出第 2 小节代码的问题。

比如,主流的 MacBook 无线网卡的名称是 en0,只要把命令 ifconfig 替换成 ifconfig en0 即可。

可问题又来了,如果使用者对 Python 不熟,他就有极大的概率,错写成以下这样:

def show_ip(self):
        ip_output = subprocess.check_output(['ifconfig en0']) # 语法错误

正确写法:subprocess.check_output(['ifconfig', 'en0'])

4、正确代码

最后,附上正确代码:

from tkinter import Tk, Label, Canvas, Button
import subprocess


class My_GUI:
    def __init__(self):
        self.root = Tk()
        self.root.geometry('800x650+200+200')
        self.root.config(bg='skyblue')
        self.root.title('Welcome to Tkinter program')

        self.label = Label(self.root, text='IP 地址查看器', fg='white',
                           bg='skyblue', width=40, font='Klee 60 bold')
        self.label.pack()

        self.canvas = Canvas(self.root, bg='white', width=600, height=400)
        self.canvas.pack()

        self.btn_show_ip = Button(
            self.root, text='点击显示本机 IP ', command=self.show_ip_MacOS)
        self.btn_show_ip.pack(pady=10)

        self.root.mainloop()

    def show_ip_MacOS(self):

        ip_output = subprocess.check_output(['ifconfig', 'en0']) # 正确写法
        ip_output = ip_output.decode('utf-8')
        ip_start_index = ip_output.find('inet ') + len('inet ')
        ip_end_index = ip_output.find(' ', ip_start_index)
        ip = ip_output[ip_start_index:ip_end_index]
        self.canvas.create_text(
            300, 200, text="该电脑无线网卡的 IP地址 为: " + ip, font=("Helvetica", 16), fill="black")


# 测试
gui = My_GUI()

四、总结

综上,便可以看出,当前的 Text 生成式 AI,是重度依赖高精度 Prompt 的上下文阐述的。

因此,不懂 Python 的用户,必然会因为 Prompt 问题而抓瞎

© 版权声明

相关文章

暂无评论

暂无评论...