OOP对小脚本有意义吗? [英] Does OOP make sense for small scripts?

查看:74
本文介绍了OOP对小脚本有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要用python写一些小的脚本,大约50-250行代码.我通常不使用任何对象,而只是简单的过程编程.

I mostly write small scripts in python, about 50 - 250 lines of code. I usually don't use any objects, just straightforward procedural programming.

我了解OOP的基础知识,并且我以前在其他编程语言中使用过对象,但是对于小型脚本,我看不到对象如何改善它们.但这也许只是我对OOP的有限经验.

I know OOP basics and I have used object in other programming languages before, but for small scripts I don't see how objects would improve them. But maybe that is just my limited experience with OOP.

我是不是通过更努力地使用对象而错过了某些东西吗?或者,对于小脚本而言,OOP是否没有太大意义?

Am I missing something by not trying harder to use objects, or does OOP just not make a lot of sense for small scripts?

推荐答案

我使用最适合当前问题的任何范式-无论是程序,OOP,功能,...程序大小都不是标准,一点点的利润)较大的程序可能更可能会利用OOP的优势-一个类的多个实例,子类和重写,特殊方法重载,OOP设计模式等.所有这些机会都可以在很小的范围内完美实现脚本,那么在较大的脚本中出现的可能性更高.

I use whatever paradigm best suits the issue at hand -- be it procedural, OOP, functional, ... program size is not a criterion, though (by a little margin) a larger program may be more likely to take advantage of OOP's strengths -- multiple instances of a class, subclassing and overriding, special method overloads, OOP design patterns, etc. Any of these opportunities can perfectly well occur in a small script, there's just a somewhat higher probability that it will occur in a larger one.

此外,我讨厌global语句,因此,如果自然程序方法需要它,则几乎总是切换到OOP -即使唯一的优势是能够使用限定名称代替裸名,需要global.

In addition, I detest the global statement, so if the natural procedural approach would require it, I will almost invariably switch to OOP instead -- even if the only advantage is the ability to use a qualified name instead of the barename which would require global.

绝对没有必要一般地加倍努力"-只是问问自己这里是否有机会使用(一个)多个实例(等)",它将很快成为第二天性,即,您将发现机会,而不必每次都自觉地提醒自己寻找机会,结果,您的编程将得到改善.

There's definitely no need to "try harder" in general -- just ask yourself "is there an opportunity here to use (a) multiple instances (etc etc)" and it will soon become second nature, i.e., you'll spot the opportunities without needing to consciously remind yourself every time to look for them, and your programming will improve as a result.

这篇关于OOP对小脚本有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆