如何使用 Python 进行大规模开发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/236407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:41:56  来源:igfitidea点击:

How can I use Python for large scale development?

pythondevelopment-environment

提问by e-satis

I would be interested to learn about large scale development in Python and especially in how do you maintain a large code base?

我有兴趣了解 Python 的大规模开发,尤其是如何维护大型代码库?

  • When you make incompatibility changes to the signature of a method, how do you find all the places where that method is being called. In C++/Java the compiler will find it for you, how do you do it in Python?

  • When you make changes deep inside the code, how do you find out what operations an instance provides, since you don't have a static type to lookup?

  • How do you handle/prevent typing errors (typos)?

  • Are UnitTest's used as a substitute for static type checking?

  • 当您对方法的签名进行不兼容更改时,您如何找到调用该方法的所有位置。在 C++/Java 中,编译器会为你找到它,你如何在 Python 中做到这一点?

  • 当您在代码内部进行更改时,您如何找出实例提供的操作,因为您没有要查找的静态类型?

  • 您如何处理/防止打字错误(错别字)?

  • UnitTest 是否用作静态类型检查的替代品?

As you can guess I almost only worked with statically typed languages (C++/Java), but I would like to try my hands on Python for larger programs. But I had a very bad experience, a long time ago, with the clipper (dBase) language, which was also dynamically typed.

你可以猜到我几乎只使用静态类型语言(C++/Java),但我想尝试使用 Python 来处理更大的程序。但是很久以前,我在使用 Clipper (dBase) 语言时遇到了非常糟糕的经历,该语言也是动态类型的。

采纳答案by Thomas Wouters

Since nobody pointed out pychecker, pylint and similar tools, I will: pychecker and pylint are tools that can help you find incorrect assumptions (about function signatures, object attributes, etc.) They won't find everything that a compiler might find in a statically typed language -- but they can find problems that such compilers for such languages can't find, too.

由于没有人指出 pychecker、pylint 和类似工具,我将:pychecker 和 pylint 是可以帮助您找到错误假设(关于函数签名、对象属性等)的工具。它们不会找到编译器可能在静态类型语言——但他们也可以找到此类语言的编译器无法找到的问题。

Python (and any dynamically typed language) is fundamentally different in terms of the errors you're likely to cause and how you would detect and fix them. It has definite downsides as well as upsides, but many (including me) would argue that in Python's case, the ease of writing code (and the ease of making it structurally sound) and of modifying code withoutbreaking API compatibility (adding new optional arguments, providing different objects that have the same set of methods and attributes) make it suitable just fine for large codebases.

Python(以及任何动态类型语言)在您可能导致的错误以及您将如何检测和修复它们方面有着根本的不同。它有明显的缺点和优点,但许多人(包括我)会争辩说,在 Python 的情况下,编写代码的容易(以及使其结构合理的容易)和修改代码而不破坏 API 兼容性(添加新的可选参数) ,提供具有相同方法和属性集的不同对象)使其非常适合大型代码库。

回答by e-satis

Don't use a screw driver as a hammer

不要用螺丝刀当锤子

Python is not a statically typed language, so don't try to use it that way.

Python 不是静态类型语言,所以不要尝试那样使用它。

When you use a specific tool, you use it for what it has been built. For Python, it means:

当您使用特定工具时,您将其用于已构建的内容。对于 Python,这意味着:

  • Duck typing: no type checking. Only behavior matters. Therefore your code must be designed to use this feature. A good design means generic signatures, no dependences between components, high abstraction levels.. So if you change anything, you won't have to change the rest of the code. Python will not complain either, that what it has been built for. Types are not an issue.

  • Huge standard library. You do not need to change all your calls in the program if you use standard features you haven't coded yourself. And Python come with batteries included. I keep discovering them everyday. I had no idea of the number of modules I could use when I started and tried to rewrite existing stuff like everybody. It's OK, you can't get it all right from the beginning.

  • 鸭子打字:没有类型检查。只有行为很重要。因此,您的代码必须设计为使用此功能。一个好的设计意味着通用签名、组件之间没有依赖关系、高抽象级别......所以如果你改变任何东西,你就不必改变其余的代码。Python 也不会抱怨它的构建目的。类型不是问题。

  • 庞大的标准库。如果您使用自己未编码的标准功能,则无需更改程序中的所有调用。Python 附带电池。我每天都在不断地发现它们。当我开始并试图像每个人一样重写现有的东西时,我不知道我可以使用多少模块。没关系,你不能一开始就搞定。

You don't write Java, C++, Python, PHP, Erlang, whatever, the same way. They are good reasons why there is room for each of so many different languages, they do not do the same things.

你不会以同样的方式编写 Java、C++、Python、PHP、Erlang 等等。它们是许多不同语言中的每一种都有空间的充分理由,它们不做相同的事情。

Unit tests are not a substitute

单元测试不能替代

Unit tests must be performed with any language. The most famous unit test library (JUnit) is from the Java world!

必须使用任何语言执行单元测试。最著名的单元测试库(JUnit)来自 Java 世界!

This has nothing to do with types. You check behaviors, again. You avoid trouble with regression. You ensure your customer you are on tracks.

这与类型无关。你再次检查行为。您可以避免回归带来的麻烦。您确保您的客户走在正轨上。

Python for large scale projects

用于大型项目的 Python

Languages, libraries and frameworks don't scale. Architectures do.

语言、库和框架不可扩展。架构可以。

If you design a solid architecture, if you are able to make it evolves quickly, then it will scale. Unit tests help, automatic code check as well. But they are just safety nets. And small ones.

如果你设计了一个坚实的架构,如果你能够让它快速发展,那么它就会扩展。单元测试有帮助,自动代码检查也是如此。但它们只是安全网。还有小的。

Python is especially suitable for large projects because it enforces some good practices and has a lot of usual design patterns built-in. But again, do not use it for what it is not designed. E.g : Python is not a technology for CPU intensive tasks.

Python 特别适用于大型项目,因为它强制执行了一些良好的实践,并且内置了许多常用的设计模式。但同样,不要将它用于非设计用途。例如:Python 不是 CPU 密集型任务的技术。

In a huge project, you will most likely use several different technologies anyway. As a SGBD(French for DBMS) and a templating language, or else. Python is no exception.

在一个庞大的项目中,您很可能无论如何都会使用几种不同的技术。作为SGBDDBMS 的法语)和模板语言,或者其他。Python也不例外。

You will probably want to use C/C++ for the part of your code you need to be fast. Or Java to fit in a Tomcatenvironment. Don't know, don't care. Python can play well with these.

您可能希望将 C/C++ 用于需要快速运行的代码部分。或 Java 以适应Tomcat环境。不知道,不关心。Python 可以很好地处理这些。

As a conclusion

作为结论

My answer may feel a bit rude, but don't get me wrong: this is a very good question.

我的回答可能有点粗鲁,但不要误会我的意思:这是一个很好的问题。

A lot of people come to Python with old habits. I screwed myself trying to code Java like Python. You can, but will never get the best of it.

很多人带着旧习惯来到 Python。我把自己搞砸了,试图像 Python 一样编写 Java 代码。你可以,但永远不会得到最好的。

If you have played / want to play with Python, it's great! It's a wonderful tool. But just a tool, really.

如果你玩过/想玩 Python,那就太棒了!这是一个很棒的工具。但只是一个工具,真的。

回答by rony l

I had some experience with modifying "Frets On Fire", an open source python "Guitar Hero" clone.

我在修改“Frets On Fire”方面有一些经验,这是一个开源 python“吉他英雄”克隆。

as I see it, python is not really suitable for a really large scale project.

在我看来,python 并不适合真正的大型项目。

I found myself spending a large part of the development time debugging issues related to assignment of incompatible types, things that static typed laguages will reveal effortlessly at compile-time. also, since types are determined on run-time, trying to understand existing code becomes harder, because you have no idea what's the type of that parameter you are currently looking at.

我发现自己花费了大部分开发时间来调试与不兼容类型分配相关的问题,静态类型语言在编译时会毫不费力地揭示这些问题。此外,由于类型是在运行时确定的,因此尝试理解现有代码变得更加困难,因为您不知道当前正在查看的该参数的类型是什么。

in addition to that, calling functions using their name string with the __getattr__built in function is generally more common in Python than in other programming languages, thus getting the call graph to a certain function somewhat hard (although you can call functions with their name in some statically typed languages as well).

除此之外,__getattr__在 Python 中使用带有内置函数的名称字符串调用函数通常比在其他编程语言中更常见,因此使调用图对某个函数有些困难(尽管您可以在某些情况下使用它们的名称调用函数)静态类型语言也是如此)。

I think that Python really shines in small scale software, rapid prototype development, and gluing existing programs together, but I would not use it for large scale software projects, since in those types of programs maintainability becomes the real issue, and in my opinion python is relatively weak there.

我认为 Python 在小型软件、快速原型开发和将现有程序粘合在一起方面确实很出色,但我不会将它用于大型软件项目,因为在这些类型的程序中,可维护性成为真正的问题,在我看来 python那里比较弱。

回答by Kozyarchuk

Here are some items that have helped me maintain a fairly large system in python.

这里有一些帮助我在 python 中维护一个相当大的系统的项目。

  • Structure your code in layers. i.e separate biz logic, presentation logic and your persistence layers. Invest a bit of time in defining these layers and make sure everyone on the project is brought in. For large systems creating a framework that forces you into a certain way of development can be key as well.

  • Tests are key, without unit tests you will likely end up with an unmanagable code base several times quicker than with other languages. Keep in mind that unit tests are often not sufficient, make sure to have several integration/acceptance tests you can run quickly after any major change.

  • Use Fail Fastprinciple. Add assertions for cases you feel your code maybe vulnerable.

  • Have standard logging/error handling that will help you quickly navigate to the issue

  • Use an IDE( pyDev works for me) that provides type ahead, pyLint/Checker integration to help you detect common typos right away and promote some coding standards

  • Carefull about your imports, never do from x import * or do relative imports without use of .

  • Do refactor, a search/replace tool with regular expressions is often all you need to do move methods/class type refactoring.

  • 分层构建您的代码。即分离业务逻辑、表示逻辑和您的持久层。花点时间定义这些层,并确保项目中的每个人都参与进来。对于大型系统,创建一个框架来强制您采用某种开发方式也很关键。

  • 测试是关键,如果没有单元测试,您可能会以比其他语言快几倍的速度结束无法管理的代码库。请记住,单元测试通常是不够的,确保有几个集成/验收测试,您可以在任何重大更改后快速运行。

  • 使用快速失败原则。为您认为代码可能存在漏洞的情况添加断言。

  • 具有标准的日志记录/错误处理,可帮助您快速定位到问题

  • 使用提供提前输入、pyLint/Checker 集成的 IDE(pyDev 对我有用)来帮助您立即检测常见的拼写错误并推广一些编码标准

  • 小心你的导入,永远不要做 from x import * 或不使用 .

  • 进行重构,带有正则表达式的搜索/替换工具通常是您进行移动方法/类类型重构所需的全部。

回答by Blauohr

my 0.10 EUR:

我的 0.10 欧元:

i have several python application in 'production'-state. our company use java, c++ and python. we develop with the eclipse ide (pydev for python)

我有几个处于“生产”状态的 Python 应用程序。我们公司使用java、c++和python。我们使用 eclipse ide (pydev for python) 进行开发

unittests are the key-solution for the problem.(also for c++ and java)

单元测试是解决问题的关键。(也适用于 c++ 和 java)

the less secure world of "dynamic-typing" will make you less careless about your code quality

不太安全的“动态类型”世界会让你不那么关心你的代码质量

BY THE WAY:

顺便说一句

large scale development doesn't mean, that you use one single language!

大规模开发并不意味着您只使用一种语言!

large scale development often uses a handful of languages specific to the problem.

大规模开发通常使用少数特定于问题的语言

so i agree to the-hammer-problem:-)

所以我同意锤子问题:-)



PS: static-typing & python

PS:静态类型和python

回答by S.Lott

Incompatible changes to the signature of a method.This doesn't happen as much in Python as it does in Java and C++.

对方法签名的不兼容更改。这在 Python 中不会像在 Java 和 C++ 中那样发生。

Python has optional arguments, default values, and far more flexibility in defining method signatures. Also, duck typing means that -- for example -- you don't have to switch from some class to an interface as part of a significant software change. Things just aren't as complex.

Python 具有可选参数、默认值,并且在定义方法签名方面具有更大的灵活性。此外,鸭子类型意味着——例如——作为重大软件更改的一部分,您不必从某个类切换到一个接口。事情没有那么复杂。

How do you find all the places where that method is being called?grep works for dynamic languages. If you need to know every place a method is used, grep (or equivalent IDE-supported search) works great.

您如何找到调用该方法的所有位置?grep 适用于动态语言。如果您需要知道使用方法的每个地方,grep(或等效的 IDE 支持的搜索)效果很好。

How do you find out what operations an instance provides, since you don't have a static type to lookup?

由于您没有要查找的静态类型,您如何找出实例提供的操作?

a. Look at the source. You don't have the Java/C++ problem of object libraries and jar files to contend with. You don't need all the elaborate aids and tools that those languages require.

一个。看源码。您无需处理对象库和 jar 文件的 Java/C++ 问题。您不需要那些语言所需的所有精心制作的辅助工具和工具。

b. An IDE can provide signature information under many common circumstances. You can, easily, defeat your IDE's reasoning powers. When that happens, you should probably review what you're doing to be sure it makes sense. If your IDE can't reason out your type information, perhaps it's too dynamic.

湾 IDE 可以在许多常见情况下提供签名信息。您可以轻松击败 IDE 的推理能力。发生这种情况时,您可能应该检查一下您正在做的事情,以确保它有意义。如果您的 IDE 无法推断出您的类型信息,则可能是它太动态了。

c. In Python, you often work through the interactive interpreter. Unlike Java and C++, you can explore your instances directly and interactively. You don't need a sophisticated IDE.

C。在 Python 中,您经常使用交互式解释器。与 Java 和 C++ 不同,您可以直接和交互地探索您的实例。您不需要复杂的 IDE。

Example:

例子:

  >>> x= SomeClass()
  >>> dir(x)

How do you handle/prevent typing errors?Same as static languages: you don't prevent them. You find and correct them. Java can only find a certain class of typos. If you have two similar class or variable names, you can wind up in deep trouble, even with static type checking.

你如何处理/防止打字错误?与静态语言相同:您不会阻止它们。你找到并纠正它们。Java 只能找到某一类的错别字。如果您有两个相似的类名或变量名,即使使用静态类型检查,也可能会遇到很大的麻烦。

Example:

例子:

class MyClass { }
class MyClassx extends MyClass { }

A typo with these two class names can cause havoc. ["But I wouldn't put myself in that position with Java," folks say. Agreed. I wouldn't put myself in that position with Python, either; you make classes that are profoundly different, and will fail early if they're misused.]

这两个类名的拼写错误可能会造成严重破坏。[“但我不会将自己置于 Java 的那个位置,”人们说。同意。我也不会将自己置于 Python 的那个位置;你创建的课程完全不同,如果它们被滥用就会过早失败。]

Are UnitTest's used as a substitute for static type checking?Here's the other Point of view: static type checking is a substitute for clear, simple design.

UnitTest 是否用作静态类型检查的替代品?这是另一个观点:静态类型检查是清晰、简单设计的替代品。

I've worked with programmers who weren't sure why an application worked. They couldn't figure out why things didn't compile; the didn't know the difference between abstract superclass and interface, and the couldn't figure out why a change in place makes a bunch of other modules in a separate JAR file crash. The static type checking gave them false confidence in a flawed design.

我曾与不确定应用程序为何有效的程序员一起工作。他们无法弄清楚为什么事情没有通过;不知道抽象超类和接口之间的区别,并且无法弄清楚为什么一个地方的变化会导致一个单独的 JAR 文件中的一堆其他模块崩溃。静态类型检查让他们对有缺陷的设计产生了错误的信心。

Dynamic languages allow programs to be simple. Simplicity is a substitute for static type checking. Clarity is a substitute for static type checking.

动态语言使程序变得简单。简单性是静态类型检查的替代品。Clarity 是静态类型检查的替代品。

回答by unintentionally left blank

My general rule of thumb is to use dynamic languages for small non-mission-critical projects and statically-typed languages for big projects. I find that code written in a dynamic language such as python gets "tangled" more quickly. Partly that is because it is much quicker to write code in a dynamic language and that leads to shortcuts and worse design, at least in my case. Partly it's because I have IntelliJ for quick and easy refactoring when I use Java, which I don't have for python.

我的一般经验法则是对小型非关键任务项目使用动态语言,对大型项目使用静态类型语言。我发现用诸如 python 之类的动态语言编写的代码会更快地“纠结”起来。部分原因是因为用动态语言编写代码要快得多,这会导致捷径和更糟糕的设计,至少在我的情况下。部分原因是我有 IntelliJ 可以在我使用 Java 时快速轻松地重构,而 Python 没有。

回答by fgg

The usual answer to that is testing testing testing. You're supposed to have an extensive unit test suite and run it often, particularly before a new version goes online.

通常的答案是测试测试测试。您应该拥有一个广泛的单元测试套件并经常运行它,尤其是在新版本上线之前。

Proponents of dynamically typed languages make the case that you have to test anyway because even in a statically typed language conformance to the crude rules of the type system covers only a small part of what can potentially go wrong.

动态类型语言的支持者认为您无论如何都必须进行测试,因为即使在静态类型语言中,对类型系统粗略规则的一致性也只涵盖了可能出错的一小部分。