简体   繁体   English

您更喜欢Python到严格的OO语言类型的应用程序或问题?

[英]What classes of applications or problems do you prefer Python to strictly OO Languages?

I've got a pretty strong background in C-style languages. 我在C风格的语言方面有很强的背景。 And have worked on several different types of projects. 并参与了几个不同类型的项目。 I have just started taking a serious look at Python after reading Programming Collective Intelligence . 在阅读编程集体智慧之后,我刚开始认真研究Python。 I understand that Python can solve any problem that C# can, and vice-versa. 我知道Python可以解决C#可以解决的任何问题,反之亦然。 But I am curious to know from those who use both regularly, when they choose one over the other. 但我很好奇地从那些经常使用它们的人那里知道,当他们选择一个而不是另一个。 Removing other factors, like coworkers experience, etc. 删除其他因素,如同事体验等。

When do you choose to create an application in Python instead of a static typed, purely OO language like C# or Java? 您何时选择使用Python创建应用程序而不是静态类型的纯OO语言(如C#或Java)?

Edit: 编辑:

I was afraid we were going to get off topic a bit with this question. 我担心我们会在这个问题上稍微讨论一下话题。 Python is an object oriented language. Python是一种面向对象的语言。 But, as is stated below it may not be the preferred language when your application will have a very heavy business domain, etc. I am aware that Python uses objects extensively, and that even functions are objects, something that is not true in all of the "OO" languages I brought up earlier. 但是,如下所述,当您的应用程序具有非常繁重的业务域等时,它可能不是首选语言。我知道Python广泛使用对象,甚至函数都是对象,这在某些情况下都不是真的。我之前提到的“OO”语言。

Despite my poor choice of words in the question (almost no languages fit nicely into two or three word descriptions, and it is really difficult to point out differences in languages without it appearing that you are talking down to a certain class of developer.), I am still very interested in what you have to say about when you prefer Python to other languages. 尽管我在问题中选择的单词很糟糕(几乎没有语言可以很好地融入两三个单词的描述中,但如果没有它出现在某种类型的开发人员面前,很难指出语言的差异。),当您更喜欢Python到其他语言时,我仍然非常感兴趣。

My motto is (and has long been) "Python where I can, C++ where I must" (one day I'll find opportunity to actually use Java, C#, &c &C, in a real-world project, but I haven't yet, except for a pilot project in Java 1.1, more tha ten years ago...;-) -- Javascript (with dojo) when code has to run in the client's browser, and SQL when it has to run in the DB server, of course, but C++ and Python are my daily bread on the "normal" servers and clients I develop, and that's the case in all parts of Google I've been working in in 4+ years (there are many parts using Java, too, I just never happened to work there;-). 我的座右铭是(并且长期以来)“我可以使用Python,我必须使用C ++”(有一天我会在实际项目中找到实际使用Java,C#和c&C的机会,但我还没有然而,除了Java 1.1中的试点项目,十年前更多... ;-) - 当代码必须在客户端的浏览器中运行时的Javascript(带有dojo),以及必须在数据库服务器中运行的SQL当然,但是C ++和Python是我开发的“普通”服务器和客户的日常面包,谷歌的所有部分都是这种情况我已经工作了4年多了(有许多部分使用Java,我也从未碰巧在那里工作;-)。 Hmmm, there's pure C when I'm working on the Python core and related extensions, too, of course;-). 嗯,当我正在研究Python核心和相关扩展时,还有纯C,当然;-)。

Neither Python nor C++ are "strictly OO" -- they're multi-paradigm, and therein lies a good part of their strength in the hands of programmers who are highly skilled at OO and other paradigms, such as functional, generic, declarative, and so forth. Python和C ++都不是“严格的OO” - 它们是多范式的,并且它们在OO 其他范例(如函数,泛型,声明性)等高级技能的程序员手中占据了很大的优势。等等。 I gather C# has pulled in some of each of these too (sometimes surpassing C++, eg by offering lambdas), and even Java has had to succumb to some (at least generic) to a tiny extent, so surely it's clear that "one size fits all" doesn't -- multi-paradigm programming is alive and well!-) 我认为C#已经引入了其中的一些(有时超过C ++,例如通过提供lambdas),甚至Java也不得不屈服于某些(至少是通用的)到一定程度,所以肯定很明显“一个尺寸”适合所有“ - 多范式编程活得很好! - )

C++ (like C) forces me to control all memory carefully (our internal c++ style guide forbids the use of "smart pointers" that amount to poor implementations of garbage collection!-), which multiplies my work a lot, but helps ensure I'm not using one bit of memory more than strictly needed at any time: so, C++ (or C when needed) is the choice when memory is tight and precious. C ++(比如C)迫使我小心控制所有内存(我们的内部c ++风格指南禁止使用“智能指针”,相当于垃圾收集的糟糕实现! - ),这使我的工作倍增,但有助于确保我' m在任何时候都不会使用超过严格需要的一位内存:因此,当内存紧凑且珍贵时,C ++(或在需要时为C)是选择。 Otherwise, the extremely high productivity of Python (and Ruby or Javascript aren't all that different, if that's what you are used to) makes it preferable. 否则,Python的极高生产力(以及Ruby或Javascript并没有那么不同,如果这是你习惯的那样)使它更受欢迎。

I'm sure there IS a niche in-between for a language that's garbage collected but mostly static, like Java (or C# before it started piling on more and more features, including dynamic ones in 4.0, I hear), or else those languages and cognate ones wouldn't be so widespread -- I've just never found myself inhabiting that peculiar niche, as yet. 对于垃圾收集但大部分是静态的语言,我认为中间存在一个利基,比如Java(或C#开始堆积越来越多的功能,包括4.0中的动态功能,我听说),或者那些语言而同源的也不会如此普遍 - 我从未发现自己居住在那个特殊的利基市场。

I select Python as often as possible. 我尽可能经常选择Python。 It is the most useful and productive programming environment that I know of. 它是我所知道的最有用和最有效的编程环境。

If I run into projects where Python cannot be used directly or for the entire project (for instance a .NET-based app. server) my approach is usually to do as much with Python as possible. 如果我遇到无法直接使用Python或整个项目(例如基于.NET的应用程序服务器)的项目,我的方法通常是尽可能多地使用Python。 Depending on the situation that might mean: 视情况而定:

  • Embed a python interpreter 嵌入了一个python解释器
  • Use Jython 使用Jython
  • Use IronPython 使用IronPython
  • Use some IPC mechanism (usually http or sockets) to call an external python process 使用一些IPC机制(通常是http或套接字)来调用外部python进程
  • Export data - process using python - import data 导出数据 - 使用python进程 - 导入数据
  • Generate code using Python 使用Python生成代码

From my answer to a previous question: I know C#. 对前一个问题的回答: 我知道C#。 Will I be more productive with Python? 我会用Python提高效率吗?


In my experience, what makes me more productive in Python vs. C#, is: 根据我的经验,使我在Python与C#中更高效的是:

  • It is a dynamic language. 这是一种动态语言。 Using a dynamic language often allows you to remove whole architectural layers from your app. 使用动态语言通常允许您从应用程序中删除整个架构图层。 Pythons dynamic nature allows you to create reusable high-level abstractions in more natural and flexible (syntax-wise) way than you can in C#. Pythons动态特性允许您以比C#更自然和灵活(语法方式)的方式创建可重用的高级抽象。
  • Libraries. 库。 The standard libraries and a lot of the open-source libraries provided by the community are of high quality. 社区提供的标准库和许多开源库都是高质量的。 The range of applications that Python is used for means that the range of libraries is wide. Python用于的应用程序范围意味着库的范围很广。
  • Faster development cycle. 更快的开发周期。 No compile step means I can test changes faster. 没有编译步骤意味着我可以更快地测试更改。 For instance, when developing a web app, the dev server detects changes and reloads the app when the files are saved. 例如,在开发Web应用程序时,开发服务器会检测更改并在保存文件时重新加载应用程序。 Running a unit test from within my editor is just a keystroke away, and executes instantaneously. 从我的编辑器中运行单元测试只是一个按键,并立即执行。
  • 'Easy access' to often-used features: lists, list comprehensions, generators, tuples etc. “轻松访问”常用功能:列表,列表推导,生成器,元组等。
  • Less verbose syntax. 更简洁的语法。 You can create a WSGI-based Python web framework in fewer lines of code than your typical .NET web.config file :-) 您可以用比典型的.NET web.config文件更少的代码行创建基于WSGI的Python Web框架:-)
  • Good documentation. 好文档。 Good books. 好书。

I almost exclusively use Python to support my development of software in other languages. 我几乎完全使用Python来支持我用其他语言开发软件。 I should stress, this is not a result of some failing in Python, rather that the software domains I am working in tend to have other languages/frameworks that are more appropriate or simply the only option: 我应该强调,这不是Python中某些失败的结果,而是我正在使用的软件域倾向于使用更合适的其他语言/框架,或者只是唯一的选择:

  • Web Development : I would love to check out Python on Google App Engine, but at the moment I am doing all my personal web development in PHP. Web开发:我很想在Google App Engine上查看Python,但目前我正在使用PHP进行所有个人Web开发。
  • Desktop Application Development : I use the Ogre SDK for developing windows screensavers and use C++/Win32 for that. 桌面应用程序开发:我使用Ogre SDK开发Windows屏幕保护程序并使用C ++ / Win32。
  • Server Application Development : Writing server side software for Windows professionally is almost always in C++. 服务器应用程序开发:专业地为Windows编写服务器端软件几乎总是使用C ++。

However, for all of these application domains I use Python regularly write tools, process data and generally to streamline my development efforts. 但是,对于所有这些应用程序域,我使用Python定期编写工具,处理数据并通常简化我的开发工作。 A few examples here are probably the best way to describe how I tend to use Python: 这里的一些例子可能是描述我倾向于使用Python的最佳方式:

  • To scape data from existing websites. 从现有网站获取数据
  • Generate reports based on XML data. 根据XML数据生成报告
  • Generate sets of SQL queries to populate databases based on other data formats. 生成一组SQL查询以基于其他数据格式填充数据库。
  • Parse entire C++ projects and pull out a distinct set of error messages and their corresponding error codes. 解析整个C ++项目并提取一组不同的错误消息及其相应的错误代码。
  • Compare data sets to find data that I have inadvertantly lost. 比较数据集以查找我无意中丢失的数据。
  • Image processing to generate data for other sotware. 图像处理以生成其他软件的数据。

Python is such an empowering and useful language that although I have never used it as the primary language for software development, I would like to. Python是一种赋权和有用的语言,虽然我从未将它用作软件开发的主要语言,但我想。

JavaScript and Python have affected how I program even in C now. JavaScript和Python已经影响了我现在甚至在C中的编程方式。 I think the best thing about knowing multiple languages is that you get more tools to use mentally, because you often don't have a choice of which language to use. 我认为了解多种语言的最佳方法是,您可以获得更多精神上使用的工具,因为您通常无法选择使用哪种语言。

Python is much more strictly OO than Java and C#. Python比Java和C#严格要求OO。

But if your question is when to use Python and when Java or C#, I find Python useful for small programs that build on existing libraries and don't involve much domain modelling. 但是如果您的问题是何时使用Python以及何时使用Java或C#,我发现Python对于基于现有库构建且不涉及太多域建模的小程序非常有用。 For example, little desktop utilities written with the Python Gtk bindings or website maintenance scripts written with lxml and elementtree. 例如,使用Python Gtk绑定编写的小桌面实用程序或使用lxml和elementtree编写的网站维护脚本。

When there is a lot of application domain modelling to do, especially if the domain is poorly understood or changing rapidly, I find Python's limited tooling makes changing the code very arduous compared to Java (not so relevant for C# because .NET tool support trails Java by a few years). 当有很多应用程序领域建模要做时,特别是如果领域知之甚少或变化不快,我发现Python的有限工具使得与Java相比更改代码非常艰巨(与C#不太相关,因为.NET工具支持Java几年)。 So for projects like that I'll use Java and IntelliJ. 所以对于那样的项目,我将使用Java和IntelliJ。

Both C and python are my languages of choice, but I almost always start doing something in python for correctness, and then dive into C when needed. C和python都是我选择的语言,但我几乎总是在python中开始做正确的事情,然后在需要时潜入C语言。 I am mostly using programming for research/numerical code, where the specifications keep changing, and C is an awful language for prototyping (this is true for most statically typed languages in my experience). 我主要使用编程用于研究/数字代码,其中规范不断变化,C是原型设计的一种糟糕的语言(根据我的经验,这对大多数静态类型的语言都是如此)。 When you have something working in C, you rarely change it significantly so that it is 'better', because you don't have the time. 如果你有一些在C中工作的东西,你很少会改变它以使它“更好”,因为你没有时间。 But sometimes, C is easier than python when you need to control resources (be it CPU, memory, etc...). 但有时候,当你需要控制资源(无论是CPU,内存等等)时,C比python更容易。

So the question really is "when is python not enough for the task", rather than the contrary. 所以问题实际上是“什么时候python不足以完成任务”,而不是相反。

Generally the language is dictated by the job, who wants the stuff done and who you are working with. 一般来说,语言取决于工作,谁想要完成的工作以及你正在与谁合作。 I only use java and c/c++ for my programming needs, mainly because the people i work with use it. 我只使用java和c / c ++来满足我的编程需求,主要是因为我工作的人使用它。 That being said ive used python for fast prototyping and such. 据说我已经使用python进行快速原型设计等。

All of them. 所有这些。

Except for code that is already written in different language, obviously. 显然,除了已经用不同语言编写的代码。

Even if something seems too big for python to handle, I usually make Python prototype anyway, mainly because it's goes so smooth. 即使某些内容对于python而言似乎太大了,我通常也会制作Python原型,主要是因为它非常流畅。 Often I'll stick with Python eitherway and just use C API or ctypes to tackle bottlenecks (after I rewrite the prototype in a nice clean manner, that is). 通常我会坚持使用Python,只需使用C API或ctypes来解决瓶颈问题(在我以一种干净的方式重写原型之后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关系数据库和OO语言 - Relational databases and OO languages 你喜欢用什么成语(如果有的话)将“this”参数命名为C#中的扩展方法,为什么? - What idiom (if any) do you prefer for naming the “this” parameter to extension methods in C#, and why? 您是否更愿意返回修改后的对象? - Do you prefer to return the modified object or not? 你更喜欢EF 4中的ObjectSet或CreateQuery? - who do you prefer ObjectSet or CreateQuery in EF 4? 令我困惑的是...... .NET语言是Windows(独立)应用程序的主流语言吗? - What puzzles me…Are .NET languages the mainstream languages for Windows (standalone) applications? 将异常处理传递给单个方法时会出现什么问题? - What problems do you see with passing exception handling to a single method? 您更喜欢哪种方法签名?为什么? - Which kind of method signature do you prefer and why? 为什么在oo设计中某些模式的类具有单独的数据类 - why do some patterns have classes with seperate data classes in oo design 部署 .NET 应用程序时 - 您如何找出共享相对于运行应用程序的计算机所在的区域? - When deploying .NET applications - how do you find out what zone a share is in relative to the computer running the application? 你喜欢那些让你把“then”放在“if”之前的语言吗? - Do you like languages that let you put the “then” before the “if”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM