Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

python - 如果解释Python,什么是.pyc文件?(If Python is interpreted, what are .pyc files?)

I've been given to understand that Python is an interpreted language... However, when I look at my Python source code I see .pyc files, which Windows identifies as "Compiled Python Files". (我已经了解Python是一种解释型语言......但是,当我查看我的Python源代码时,我看到.pyc文件,Windows将其识别为“编译的Python文件”。) Where do these come in? (这些来自哪里?)

  ask by froadie translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've been given to understand that Python is an interpreted language... (我已经理解Python是一种解释语言......)

This popular meme is incorrect, or, rather, constructed upon a misunderstanding of (natural) language levels: a similar mistake would be to say "the Bible is a hardcover book". (这种流行的模因是不正确的,或者更确切地说,是基于对(自然)语言水平的误解而构建的:类似的错误就是说“圣经是精装书”。) Let me explain that simile... (让我解释一下比喻......)

"The Bible" is "a book" in the sense of being a class of (actual, physical objects identified as) books; (“圣经”是“一本书”,意思是成为一 (实际的,物理对象)书籍;) the books identified as "copies of the Bible" are supposed to have something fundamental in common (the contents, although even those can be in different languages, with different acceptable translations, levels of footnotes and other annotations) -- however, those books are perfectly well allowed to differ in a myriad of aspects that are not considered fundamental -- kind of binding, color of binding, font(s) used in the printing, illustrations if any, wide writable margins or not, numbers and kinds of builtin bookmarks, and so on, and so forth. (被确定为“圣经副本”的书籍应该具有一些基本的共同点(内容,即使是那些可以使用不同语言,具有不同的可接受翻译,脚注和其他注释的水平) - 然而,这些书籍是很好地允许在被认为是基本的无数方面有所区别 - 绑定的种类,绑定的颜色,打印中使用的字体,插图(如果有的话),宽的可写边距,内置书签的数量和种类, 等等等等。)

It's quite possible that a typical printing of the Bible would indeed be in hardcover binding -- after all, it's a book that's typically meant to be read over and over, bookmarked at several places, thumbed through looking for given chapter-and-verse pointers, etc, etc, and a good hardcover binding can make a given copy last longer under such use. (很有可能的是,圣经的典型印刷确实是精装书 - 毕竟,它是一本通常意味着一遍又一遍地阅读的书,在几个地方加书签,翻阅寻找给定的章节和诗句指针等等,一个好的精装书绑定可以使给定的副本在这种使用下持续更长时间。) However, these are mundane (practical) issues that cannot be used to determine whether a given actual book object is a copy of the Bible or not: paperback printings are perfectly possible! (然而,这些是平凡的(实际的)问题,不能用于确定给定的实际书籍对象是否是圣经的副本:平装印刷是完全可能的!)

Similarly, Python is "a language" in the sense of defining a class of language implementations which must all be similar in some fundamental respects (syntax, most semantics except those parts of those where they're explicitly allowed to differ) but are fully allowed to differ in just about every "implementation" detail -- including how they deal with the source files they're given, whether they compile the sources to some lower level forms (and, if so, which form -- and whether they save such compiled forms, to disk or elsewhere), how they execute said forms, and so forth. (类似地,Python是定义一类语言实现的 “语言”,它必须在某些基本方面都是相似的(语法,大多数语义,除了那些明确允许它们不同的那些部分)但完全允许几乎每个“实现”细节都有所不同 - 包括他们如何处理他们给出的源文件,他们是否将源代码编译成某种较低级别的形式(如果是,那么哪种形式 - 以及他们是否保存这些形式编译表格,磁盘或其他地方),他们如何执行所述表格,等等。)

The classical implementation, CPython, is often called just "Python" for short -- but it's just one of several production-quality implementations, side by side with Microsoft's IronPython (which compiles to CLR codes, ie, ".NET"), Jython (which compiles to JVM codes), PyPy (which is written in Python itself and can compile to a huge variety of "back-end" forms including "just-in-time" generated machine language). (经典实现CPython通常简称为“Python” - 但它只是几个生产质量实现中的一个,与Microsoft的IronPython(编译为CLR代码,即“.NET”)并列,Jython (编译为JVM代码),PyPy(用Python本身编写,可以编译成各种各样的“后端”形式,包括“即时”生成的机器语言)。) They're all Python (=="implementations of the Python language") just like many superficially different book objects can all be Bibles (=="copies of The Bible"). (它们都是Python(==“Python语言的实现”),就像许多表面上不同的书籍对象都可以是圣经(==“圣经的副本”)。)

If you're interested in CPython specifically: it compiles the source files into a Python-specific lower-level form (known as "bytecode"), does so automatically when needed (when there is no bytecode file corresponding to a source file, or the bytecode file is older than the source or compiled by a different Python version), usually saves the bytecode files to disk (to avoid recompiling them in the future). (如果你对CPython特别感兴趣:它将源文件编译成特定于Python的低级表单(称为“字节码”),在需要时自动执行(当没有与源文件对应的字节码文件时,或者字节码文件比源文件旧,或由不同的Python版本编译),通常将字节码文件保存到磁盘(以避免将来重新编译它们)。) OTOH IronPython will typically compile to CLR codes (saving them to disk or not, depending) and Jython to JVM codes (saving them to disk or not -- it will use the .class extension if it does save them). (OTOH IronPython通常会编译为CLR代码(将它们保存到磁盘或不依赖)和Jython保存到JVM代码(将它们保存到磁盘或不保存 - 如果保存它们将使用.class扩展名)。)

These lower level forms are then executed by appropriate "virtual machines" also known as "interpreters" -- the CPython VM, the .Net runtime, the Java VM (aka JVM), as appropriate. (然后,这些较低级别的表单由适当的“虚拟机”(也称为“解释器”)执行 - CPython VM,.Net运行时,Java VM(也称为JVM)。)

So, in this sense (what do typical implementations do), Python is an "interpreted language" if and only if C# and Java are: all of them have a typical implementation strategy of producing bytecode first, then executing it via a VM/interpreter. (因此,从这个意义上来说(典型的实现是做什么的),当且仅当C#和Java是:所有这些都具有首先生成字节码的典型实现策略,然后通过VM /解释器执行它时,Python是一种“解释语言” 。)

More likely the focus is on how "heavy", slow, and high-ceremony the compilation process is. (更有可能的焦点是编译过程的“重”,慢和高仪式。) CPython is designed to compile as fast as possible, as lightweight as possible, with as little ceremony as feasible -- the compiler does very little error checking and optimization, so it can run fast and in small amounts of memory, which in turns lets it be run automatically and transparently whenever needed, without the user even needing to be aware that there is a compilation going on, most of the time. (CPython旨在尽可能快地编译,尽可能轻量级,尽可能少的仪式 - 编译器执行非常少的错误检查和优化,因此它可以快速运行并且在少量内存中运行,这样就可以了可以在需要时自动且透明地运行,而无需用户甚至需要知道正在进行编译,大多数情况下。) Java and C# typically accept more work during compilation (and therefore don't perform automatic compilation) in order to check errors more thoroughly and perform more optimizations. (Java和C#通常在编译期间接受更多工作(因此不执行自动编译),以便更彻底地检查错误并执行更多优化。) It's a continuum of gray scales, not a black or white situation, and it would be utterly arbitrary to put a threshold at some given level and say that only above that level you call it "compilation"!-) (它是灰度级的连续体,而不是黑色或白色的情况,并且将阈值置于某个给定的水平并且仅在该级别之上将其称为“编译”将是完全随意的! - ))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...