m5k1umn 发表于 2024-8-17 07:42:19

Python 到底是强类型语言,还是弱类型语言?


    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/39f21bb1c067453697ab1f052dec1e94~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723888404&amp;x-signature=2ejyM%2BZ%2B9BdvBzItx1ki%2FgV4U34%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">0、前言</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我在上一篇<span style="color: black;">文案</span>中分析了 <span style="color: black;">为何</span> Python <span style="color: black;">无</span> void 类型 的<span style="color: black;">专题</span>,在<span style="color: black;">文案</span>发布后,有读者跟我讨论起了另一个关于类型的问题,<span style="color: black;">然则</span>,<span style="color: black;">咱们</span><span style="color: black;">火速</span>就<span style="color: black;">显现</span>了重大<span style="color: black;">歧义</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">重点</span>的<span style="color: black;">歧义</span>就在于:<strong style="color: blue;">Python 到底是不是强类型语言?</strong> 我认为是,而他认为不是。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">他写了一篇很长的<span style="color: black;">文案</span>《谁告诉的你们Python是强类型语言!站出来,<span style="color: black;">保准</span>不打你!》,专门重申了他的观点,但可惜错漏百出。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我曾有想法要写写关于 Python 类型的问题,<span style="color: black;">此刻</span>借着这个机会,就来系统地梳理一下吧。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(PS:在我写作进行到差不多一半的时候,<span style="color: black;">微X</span>读者群里恰好<span style="color: black;">亦</span>讨论到“强弱类型”的<span style="color: black;">专题</span>!在与<span style="color: black;">大众</span>讨论时,我的<span style="color: black;">有些</span>想法得到了验证,<span style="color: black;">同期</span>我<span style="color: black;">亦</span>学到了很多新知识,<span style="color: black;">因此</span>本文的部分内容有群友们的功劳,特此鸣谢!)</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">1、动静类型与强弱类型</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">非常多</span>读者应该都<span style="color: black;">熟练</span>动态类型 与静态类型 ,<span style="color: black;">然则</span><span style="color: black;">非常多</span>人<span style="color: black;">亦</span>会把它们跟强弱类型 混为一谈,<span style="color: black;">因此</span><span style="color: black;">咱们</span>有必要先作一下概念上的澄清。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这两组类型都是针<span style="color: black;">针对</span>编程语言而言的,但关注的核心问题<span style="color: black;">区别</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">针对</span>“动静类型”概念,它的核心问题是“什么时候<span style="color: black;">晓得</span>一个变量是哪种类型”?</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">通常</span>而言,在编译期就确定变量类型的是静态类型语言,在运行期才确定变量类型的则是动态类型语言。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如,某些语言中定义函数“int func(int a){…}”,在编译时就能确定<span style="color: black;">晓得</span>它的参数和返回值是 int 类型,<span style="color: black;">因此</span>是静态类型;而典型如 Python,定义函数时写“def func(a):…”,并不<span style="color: black;">晓得</span>参数和返回值的类型,<span style="color: black;">仅有</span>到运行时调用函数,才<span style="color: black;">最后</span>确定参数和返回值的类型,<span style="color: black;">因此</span>是动态类型</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">针对</span>“强弱类型”概念,它的核心问题是“<span style="color: black;">区别</span>类型的变量<span style="color: black;">是不是</span><span style="color: black;">准许</span>隐式转化”?</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">通常</span>而言,编译器有很少(<span style="color: black;">恰当</span>)隐式类型转化的是强类型语言,有较多(过分)隐式类型转化的是弱类型语言。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如,Javascript 中的 "1000"+1 会得到字符串“10001”,而 "1000"-1 则会得到数字 999,<span style="color: black;">亦</span><span style="color: black;">便是</span>说,编译器<span style="color: black;">按照</span><span style="color: black;">运用</span>场合,对两种<span style="color: black;">区别</span>类型的对象分别做了隐式的类型转化,<span style="color: black;">然则</span><span style="color: black;">类似</span>的写法,在强类型语言中则会报类型出错。(数字与字符串的转化属于过分的转化,下文会再<span style="color: black;">说到</span><span style="color: black;">有些</span><span style="color: black;">恰当</span>的转化。)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">根据</span>以上的定义,有人将<span style="color: black;">平常</span>的编程语言画了一张<span style="color: black;">归类</span>图:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/380c2553c6244be6aa3b7a22404739ee~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723888404&amp;x-signature=IjC%2BhIP0fxmkZH9GxIr%2FAzjZUQA%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">按强弱类型维度的划分,<span style="color: black;">能够</span>归纳出:</p>强类型:Java、C#、Python、Ruby、Erlang(再加GO、Rust)……弱类型:C、C++、Javascript、Perl、PHP、VB……<h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">2、过去的强弱类型概念</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">动静类型的概念基本上被<span style="color: black;">大众</span>所认可,然而,强弱类型的概念在问答社区、技术论坛和学术讨论上却有<span style="color: black;">非常多</span>的争议。此处就不作<span style="color: black;">陈列</span>了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为何</span>会有<span style="color: black;">那样</span>多争议呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">最<span style="color: black;">重点</span>的<span style="color: black;">原由</span>之一是有人把它与动静类型混用了。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最<span style="color: black;">显著</span>的一个例子<span style="color: black;">便是</span> Guido van Rossum 在 2003 年参加的一个访谈,它的<span style="color: black;">专题</span>恰好是关于强弱类型的(Strong versus Weak Typing):</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/8794fc488fca4a6e8e1e0737879e6a7b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723888404&amp;x-signature=nsGT0jMSqiO8SnNc6FV4MUZcwYE%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,<span style="color: black;">她们</span>谈论的<span style="color: black;">显著</span>只是动静类型的区别。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">访谈中还引述了 Java 之父 James Gosling 的话,从他的表述中<span style="color: black;">亦</span>能看出,他说的“强弱类型”其实<span style="color: black;">亦</span>是动静类型的区分。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一</span>还有一个经典的例子,C 语言之父 Dennis Ritchie 曾经说 C 语言是一种<strong style="color: blue;">“强类型<span style="color: black;">然则</span>弱<span style="color: black;">检测</span>”</strong>的语言。<span style="color: black;">倘若</span>对照成前文的定义,那他其实指的是“静态类型弱类型”。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为何</span>这些大佬们会有混淆呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实<span style="color: black;">原由</span><span style="color: black;">亦</span>很简单,<strong style="color: blue;">那<span style="color: black;">便是</span>在当时还<span style="color: black;">无</span><span style="color: black;">知道</span>的动静类型与强弱类型的概念之分!<span style="color: black;">或</span>说,那时候的强弱类型指的<span style="color: black;">便是</span>动静类型。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">维基百科上给出了 1970 年代对强类型的定义,基本<span style="color: black;">能够</span>还原成前文<span style="color: black;">说到</span>的静态类型:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">In 1974, Liskov and Zilles defined a strongly-typed language as one in which "whenever an object is passed from a calling function to a called function, its type must be compatible with the type declared in the called function." In 1977, Jackson wrote, "In a strongly typed language each data area will have a distinct type and each process will state its communication requirements in terms of these types."</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前面几位编程语言之父应该<span style="color: black;">便是</span>持有类似的观念。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不外</span>,大佬们<span style="color: black;">亦</span><span style="color: black;">认识</span>到了当时的“强弱类型”概念并不充分准确,<span style="color: black;">因此</span> Dennis Ritchie 才会说成<strong style="color: blue;">“强类型<span style="color: black;">然则</span>弱<span style="color: black;">检测</span>”,</strong><span style="color: black;">况且</span>在访谈中,Guido <span style="color: black;">亦</span><span style="color: black;">尤其</span>强调了 Python <span style="color: black;">不该</span>该被<span style="color: black;">叫作</span>为弱类型,而应该说是<strong style="color: blue;">运行时类型(runtime typing)</strong> 。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>在那个<span style="color: black;">初期</span>年代,基本上强弱类型就等同于动静类型,而<span style="color: black;">这般</span>的想法<span style="color: black;">迄今</span>仍在影响着<span style="color: black;">非常多</span>人。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">3、<span style="color: black;">此刻</span>的强弱类型概念</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">初期</span><span style="color: black;">针对</span>编程语言的<span style="color: black;">归类</span>其实是混杂了动静与强弱两个维度,<span style="color: black;">然则</span>,它们并不是一一对应重合的关系,并不足以表达编程语言间的区别,<span style="color: black;">因此呢</span>就<span style="color: black;">必须</span>有更为<span style="color: black;">知道</span>/丰富的定义。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有人提出了“type safety”、“memory safety”等区分维度,<span style="color: black;">亦</span><span style="color: black;">显现</span>了静态<span style="color: black;">检测</span>类型和动态<span style="color: black;">检测</span>类型,与强弱类型存在<span style="color: black;">必定</span>的交集。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">直到<span style="color: black;">显现</span> 2004 年的一篇集大成的学术论文《Type Systems》(出自微软<span style="color: black;">科研</span>院,作者 Luca Cardelli),专门<span style="color: black;">科研</span>编程语言的<span style="color: black;">区别</span>类型系统:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/d1bc0eec9d534ff4b999364d45183a6b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723888404&amp;x-signature=C7cgbIk5efAERaVX3POhWlIBe2Y%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">论文中<span style="color: black;">针对</span>强弱<span style="color: black;">检测</span>(<span style="color: black;">亦</span>即强弱类型)有一个简短的归纳如下:</p>Strongly checked language: A language where no forbidden errors can occur at run time (depending on the definition of forbidden error).Weakly checked language: A language that is statically checked but provides no clear guarantee of absence of execution errors.<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其关键则是程序<span style="color: black;">针对</span> untrapped errors 的<span style="color: black;">检测</span>强度,在某些<span style="color: black;">实质</span>已出错的<span style="color: black;">地区</span>,弱类型程序并不作<span style="color: black;">捕捉</span>处理,例如 C 语言的<span style="color: black;">有些</span>指针计算和转换,而《C 程序员十诫》的前几个都是弱类型<span style="color: black;">引起</span>的问题。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/cc18c3b31ba740a09b0a0c3ee0682369~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723888404&amp;x-signature=lFbfUlplf3M881WwDB5WqLdzKC0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">论文<span style="color: black;">针对</span>这些概念的定义还是比较抽象的,<span style="color: black;">因为</span>未<span style="color: black;">捕捉</span>的错误(untrapped errors)大<span style="color: black;">大都是</span><span style="color: black;">因为</span>隐式类型转换<span style="color: black;">导致</span>,<span style="color: black;">因此</span>又演化出了<span style="color: black;">第1</span>节中的定义,以隐式类型转换<span style="color: black;">做为</span>判断标准。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如今将<strong style="color: blue;">“对隐式类型转换的容忍度”</strong><span style="color: black;">做为</span>强弱类型的<span style="color: black;">归类</span>标准,<span style="color: black;">已然</span>是<span style="color: black;">非常多</span>人的共识(虽然<span style="color: black;">不足</span>全面,<span style="color: black;">况且</span>有<span style="color: black;">有些</span><span style="color: black;">区别</span>的声音)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如,维基百科就把隐式类型转换<span style="color: black;">做为</span>弱类型的<span style="color: black;">重点</span>特点之一:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">A weakly typed language has looser typing rules and may produce unpredictable results or may perform implicit type conversion at runtime.</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如,以 Python 为例,<strong style="color: blue;">社区的主流看法认为它是强类型语言,而判断的标准<span style="color: black;">亦</span>是看隐式类型转换。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例子有<span style="color: black;">非常多</span>,<span style="color: black;">例如</span> Python 官方的 wiki,它专门回答了Why is Python a dynamic language and also a strongly typed language ,给出了 4 个答案,为 Python 的“动态强类型”定性:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/17ed4dbfdfa44afbb0db826076edc3bf~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723888404&amp;x-signature=Ed3mq17of6CUtPa2x09t%2BWacNOs%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">再<span style="color: black;">例如</span>,在《流畅的Python》第11章的杂谈中,<span style="color: black;">亦</span>专门<span style="color: black;">说到</span>了强弱类型的<span style="color: black;">归类</span>。(它的用语是“很少隐式类型转换”,算是比较严谨的,<span style="color: black;">然则</span><span style="color: black;">亦</span>错误地把 C++ 归为了强类型。)</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">4、Python 是不是强类型语言?</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关于“Python <span style="color: black;">是不是</span>属于强类型”<span style="color: black;">专题</span>,在主流观点之外,还存在着不少误解的看法。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一方面的<span style="color: black;">原由</span>有些人混用了强弱类型与动静类型,这有历史的<span style="color: black;">原由</span>,前面<span style="color: black;">已然</span>分析了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一</span>还有一个<span style="color: black;">一样</span>重要的<span style="color: black;">原由</span>,即有人把弱类型等同于“完全<span style="color: black;">无</span>隐式类型转换”了,这种想法并不对。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">事实上,强弱类型的概念中<span style="color: black;">包括</span>着部分相对主义的含义,强类型语言中<span style="color: black;">亦</span>可能有隐式类型转换。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span>,Rust 语言为了实现“内存安全”的设计哲学,设计了很强大的类型系统,<span style="color: black;">然则</span>它里面<span style="color: black;">亦</span>有隐式类型转换(自动解引用)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">问题在于:<strong style="color: blue;">怎么样的隐式类型转换是在<span style="color: black;">恰当</span>范围内的?以及,某些表面的隐式类型转换,<span style="color: black;">是不是</span>真的是隐式类型转换?</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">回到 Python 的例子,<span style="color: black;">咱们</span><span style="color: black;">能够</span>分析几种典型的用法。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span>,"test"*3 这种字符串“乘法”运算,虽然是两种类型的操作,<span style="color: black;">然则</span>并不<span style="color: black;">触及</span>隐式类型转换转化。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span>,x=10; x="test" 先后给一个变量<span style="color: black;">区别</span>类型的赋值,表面上看 x 的类型变化了,用 type(x) <span style="color: black;">能够</span>判断出<span style="color: black;">区别</span>,<span style="color: black;">然则</span>,Python 中的类型是跟值绑定的(右值绑定),并不是跟变量绑定的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">变量 x 准确地说只是变量名,是绑定到<span style="color: black;">实质</span>变量上的一个标签,它<span style="color: black;">无</span>类型。type(x) 判断出的并不是 x 本身的类型,而是 x 指向的对象的类型,就像内置函数 id(x) 算出的<span style="color: black;">亦</span>不是 x 本身的<span style="color: black;">位置</span>,而是<span style="color: black;">实质</span>的对象的<span style="color: black;">位置</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span>,1 + True 这种数字与布尔类型的加法运算,<span style="color: black;">亦</span><span style="color: black;">无</span><span style="color: black;">出现</span>隐式类型转换。<span style="color: black;">由于</span> Python 中的布尔类型其实是整型的子类,是同一种类型!(<span style="color: black;">倘若</span>有疑问,可查阅 PEP-285)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">再<span style="color: black;">例如</span>,整数/布尔值与浮点数相加,在 Python 中<span style="color: black;">亦</span>不<span style="color: black;">必须</span>作显式类型转换。<span style="color: black;">然则</span>,它的实现过程其实是用了数字的__add__() <span style="color: black;">办法</span>,Python 中一切皆对象,数字对象<span style="color: black;">亦</span>有自己的<span style="color: black;">办法</span>。(其它语言可不<span style="color: black;">必定</span>)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">亦</span><span style="color: black;">便是</span>说,<strong style="color: blue;">数字间的算术运算操作,其实是一个函数调用的过程,跟其它语言中的算术运算有着本质的区别。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">另一</span>,<span style="color: black;">区别</span>的数字类型虽然在计算机存储层面有很大差异,但在人类眼中,它们是同一种类型(宽泛地分),<span style="color: black;">因此</span>就算<span style="color: black;">出现</span>了隐式类型转换,在<span style="color: black;">规律</span>上<span style="color: black;">亦</span>是可以接受的。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,还有一个例子,即 Python 在 if/while 之后的真值判断,我之前分析过它的实现原理 ,它会把其它类型的对象转化成布尔类型的值。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,它<span style="color: black;">实质</span>上<span style="color: black;">亦</span>只是函数调用的结果(__bool__() 和 __len__()),是<span style="color: black;">经过</span>计算而得出的<span style="color: black;">恰当</span>结果,并不属于隐式的强制类型转换,不在 untrapped errors 的范畴里。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">因此</span>,严格<span style="color: black;">来讲</span>,前面 5 个例子中都<span style="color: black;">无</span><span style="color: black;">出现</span>类型转换。</strong> 浮点数和真值判断的例子,直观上看是<span style="color: black;">出现</span>了类型转换,但它们其实是 Python 的特性,是可控的、符合预期的、并<span style="color: black;">无</span>对原有类型<span style="color: black;">导致</span>破坏。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">退一步讲,若放宽“隐式类型转换”的含义,认为后两个例子<span style="color: black;">出现</span>了隐式类型转换,<span style="color: black;">然则</span>,它们是<span style="color: black;">经过</span>严谨的函数调用过程实现的,<span style="color: black;">亦</span>不会<span style="color: black;">显现</span> forbidden errors,<span style="color: black;">因此</span>还是属于强<span style="color: black;">检测</span>类型。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">5、其它<span style="color: black;">关联</span>的问题</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前文对概念的含义以及 Python 中的表现,作了细致的分析。接下来,为了<span style="color: black;">规律</span>与<span style="color: black;">专题</span>的完整性,<span style="color: black;">咱们</span>还<span style="color: black;">必须</span>回答几个小问题:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1)能否以“隐式类型转换”<span style="color: black;">做为</span>强弱类型的分类依据?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">知道</span>的<span style="color: black;">归类</span>定义应该以《Type Systems》为准,它有一套针对<span style="color: black;">区别</span> error 的<span style="color: black;">归类</span>,强弱类型其实是<span style="color: black;">针对</span> forbidden errors 的处理<span style="color: black;">归类</span>。隐式类型转换是其<span style="color: black;">显著</span>的特征,但并不是<span style="color: black;">所有</span>,<span style="color: black;">亦</span>不是<span style="color: black;">独一</span>的判断依据。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文为了方便理解,<span style="color: black;">运用</span>这个<span style="color: black;">重点</span>特征来划分强弱类型,<span style="color: black;">然则</span>要强调,强类型不是<span style="color: black;">无</span>隐式类型转换,而是可能有很少且<span style="color: black;">恰当</span>的隐式类型转换。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)假如有其它解释器令 Python 支持广泛的隐式类型转换,那 Python 还是强类型语言么?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语言的标准规范就像是法律,而解释器是执法者。<span style="color: black;">倘若</span>有错误的执法解释,那法律还是那个法律,应该改掉错误的执法<span style="color: black;">行径</span>;<span style="color: black;">倘若</span>是法律本身有问题(造<span style="color: black;">成为了</span>解释歧义和矛盾,<span style="color: black;">或</span>该废弃),那就应该修改法律,<span style="color: black;">保准</span>它的确定性(要么是强类型,要么是弱类型)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3)<span style="color: black;">为何</span>说 Javascript 是弱类型?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>它的隐式类型转换非常多、非常<span style="color: black;">繁杂</span>、非常过分!<span style="color: black;">例如</span>,Javascript 中123 + null 结果为 123,123 + {} 结果为字符串“123”。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一</span>,它的双等号“==”除了有基本的比较操作,还可能<span style="color: black;">出现</span>多重的隐式类型转换,例如true== 判断出的结果为 false,而true== 的结果是 true,还有[]==![] 和==false 的结果都为 true……</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(4)C++ 是不是弱类型语言?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前文<span style="color: black;">说到</span>《流畅的Python》中将 C++ 归为强类型,但<span style="color: black;">实质</span>上它应该被归为弱类型。C++ 的类型转换是个非常<span style="color: black;">繁杂</span>的<span style="color: black;">专题</span>,@樱雨楼 小姐姐曾写过一个系列<span style="color: black;">文案</span>做了系统论述,<span style="color: black;">文案</span><span style="color: black;">位置</span>:<span style="color: black;">怎样</span><span style="color: black;">解决</span> C++ 中<span style="color: black;">繁杂</span>的类型转换? 、详解 C++ 的隐式类型转换与函数重载! 、谁说 C++ 的强制类型转换很难懂?</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">6、小结</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">强弱类型概念在网上有比较多的争议,不仅在 Python 是如此,在 C/C++ 之类的语言更甚。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">其实在学术上,这个概念早已有<span style="color: black;">知道</span>的定义,<span style="color: black;">况且</span>事实上<span style="color: black;">亦</span>被<span style="color: black;">非常多</span>人所接纳。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">哪些</span>反对的声音大<span style="color: black;">大都是</span><span style="color: black;">由于</span>概念混用,<span style="color: black;">由于</span><span style="color: black;">她们</span>忽略了另一种对语言进行<span style="color: black;">归类</span>的维度;<span style="color: black;">同期</span>,还有一部分值得<span style="color: black;">重视</span>的<span style="color: black;">原由</span>,即<span style="color: black;">不可</span>认为强类型等于“完全无隐式类型转换”或“只要<span style="color: black;">无</span>xxx隐式类型转换”。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文介绍了社区中对 Python 的主流<span style="color: black;">归类</span>,<span style="color: black;">同期</span>对几类疑似隐式类型转换的用法进行了分析,论证出它是一种强类型语言。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">文案</span><span style="color: black;">表现</span>了作者一贯的刨根问底精神,这是“Python<span style="color: black;">为何</span>”系列<span style="color: black;">文案</span>的风格,<span style="color: black;">倘若</span>你<span style="color: black;">爱好</span>本文,欢迎订阅关注!</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">关联</span>链接</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 谁告诉的你们Python是强类型语言!站出来,<span style="color: black;">保准</span>不打你!:</p>https://blog.csdn.net/nokiaguy/article/details/108218260

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Strong versus Weak Typing: https://www.artima.com/intv/strongweak.html</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> https://en.wikipedia.org/wiki/Strong_and_weak_typing#cite_note-2</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> https://en.wikipedia.org/wiki/Strong_and_weak_typing#cite_note-3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Type Systems: http://lucacardelli.name/Papers/TypeSystems.pdf</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> C 程序员十诫: </p>http://doc.cat-v.org/henry_spencer/ten-commandments

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Why is Python a dynamic language and also a strongly typed language: https://wiki.python.org/moin/Why%20is%20Python%20a%20dynamic%20language%20and%20also%20a%20strongly%20typed%20language</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> PEP-285: https://github.com/chinesehuazhou/peps-cn/blob/master/StandardsTrack/285--%E6%B7%BB%E5%8A%A0%E5%B8%83%E5%B0%94%E7%B1%BB%E5%9E%8B.md</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Type Systems: http://lucacardelli.name/Papers/TypeSystems.pdf</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Python<span style="color: black;">为何</span>: </p>https://github.com/chinesehuazhou/python-whydo





九天一剑 发表于 2024-8-20 18:37:46

楼主的文章非常有意义,提升了我的知识水平。

nuanhome 发表于 2024-9-10 03:39:41

这夸赞甜到心里,让我感觉温暖无比。

nqkk58 发表于 2024-10-8 13:06:04

外链论坛的成功举办,是与各位领导、同仁们的关怀和支持分不开的。在此,我谨代表公司向关心和支持论坛的各界人士表示最衷心的感谢!

b1gc8v 发表于 2024-10-13 10:37:39

太棒了、厉害、为你打call、点赞、非常精彩等。

j8typz 发表于 2024-11-12 07:17:23

你的见解独到,让我受益匪浅,非常感谢。
页: [1]
查看完整版本: Python 到底是强类型语言,还是弱类型语言?