简体   繁体   English

不在 Python 中使用私有方法和函数是不好的做法吗?

[英]Is it bad practice NOT to use private methods and functions in Python?

Background背景

A few weeks ago, I completed a technical test in Python.几周前,我在 Python 完成了技术测试。 While the overall feedback from the test was very positive, one of the things I was surprised to read in the feedback was:虽然测试的总体反馈非常积极,但我在反馈中看到的一件令我惊讶的事情是:

No encapsulation – plenty of public_functions which should be _private_functions没有封装——大量的 public_functions 应该是 _private_functions

This surprised me because - according to the vague picture I'd built up in the back of my mind over my two years as a Python developer - we don't really do private methods or functions in Python .这让我感到惊讶,因为 - 根据我作为 Python 开发人员两年来在我脑海中建立的模糊画面 -我们并没有真正在 Python 中使用私有方法或函数 All that we do have is a patchwork of conventions, shorthand and gentleman's agreements.我们所拥有的只是各种公约、速记和君子协定的拼凑。 But is that picture in the back of my mind correct?但是我脑海中的那个画面是正确的吗?

Underscores and Double Underscores下划线和双下划线

So I've looked into the matter a little more deeply, consulting the standard Python documentation as well as PEP 8 , and I've come to the following conclusions:所以我更深入地研究了这个问题,查阅了标准的 Python 文档以及PEP 8 ,我得出了以下结论:

  1. The use of a double underscore before a class attribute invokes name mangling , ie __spam will be interpreted as _classname__spam .在 class 属性之前使用双下划线会调用name mangling ,即__spam将被解释为_classname__spam This can be used to emulate a crude kind of privacy, such that if one tries to call __spam from outside of its class, an exception will be raised.这可以用来模拟一种粗略的隐私,因此如果尝试从其 class 外部调用__spam ,则会引发异常。
  2. The programmer can use a single underscore to indicate that a function (or similar) is private, but this seems to have little syntactic effect.程序员可以使用单个下划线来表示 function (或类似的)是私有的,但这似乎没有什么语法效果。 PEP 8 mentions that _single_leading_underscore will not be imported when from M import * is called, but I couldn't find anything else. PEP 8 提到在调用from M import *时不会导入_single_leading_underscore ,但我找不到其他任何东西。

However, this only tells me what I could do, not what I should do.然而,这只是告诉我我能做什么,而不是我该做什么。

What I Want to Know我想知道的

Can I write off the above feedback as coming from someone who's probably spent his professional life programming in Java/C#/etc, and therefore doesn't have the correct mindset when it comes to encapsulation in Python?我是否可以将上述反馈视为来自可能在 Java/C#/etc 中度过职业生涯的人,因此在 Python 中的封装时没有正确的心态? Or should I re-think the way I write Python programs, so that any encapsulation is much more explicit?或者我应该重新考虑我编写 Python 程序的方式,以便任何封装更加明确?

From technical point of view, there is no right or wrong way of writing code.从技术角度来看,编写代码的方式没有对错之分。 What computer cares about are instructions and data.计算机关心的是指令和数据。 If you use clases or not, if your code is modular or spaghetti, what are the names of your functions, if they are public or private etc. - it all goes down the drain by the time it reaches the CPU.如果您使用类或不使用类,如果您的代码是模块化的或意大利面条,您的函数的名称是什么,它们是公共的还是私有的等等 - 当它到达 CPU 时,这一切都付诸东流了。

Arguments like these are arguments about opinions. Arguments 像这些是 arguments 关于意见。 Everybody has an opinion.每个人都有意见。 You might also call it taste.你也可以称之为味道。 People with similar opinions group together and create organizations, political parties, institutions etc. to promote their ideas.具有相似观点的人聚集在一起,创建组织、政党、机构等来宣传他们的想法。 They claim that it is somehow possible to generalize and distill how software should be written, into decisions that are either "right" or "wrong".他们声称,以某种方式可以概括和提炼软件的编写方式,使其成为“正确”或“错误”的决策。

Yet, for every rule that proves something, you will quickly find another rule that contradicts it.然而,对于每一条证明某事的规则,你很快就会发现另一条与之相矛盾的规则。 This is just the law of nature - reality knows no boundaries.这只是自然法则——现实没有界限。 This is the reason of all confusion.这就是一切混乱的原因。

There is no such thing as universal rule about something, and there never will be.没有关于某事的普遍规则这样的东西,而且永远不会有。 It would contradict the very nature of the universe: time and entropy.这将与宇宙的本质相矛盾:时间和熵。 Whatever question you ask, the answer is always "it depends".无论你问什么问题,答案总是“视情况而定”。

After 40 years of programming my core values are down to this:经过 40 年的编程,我的核心价值观归结为:

  • Consistency matters more than style.一致性比风格更重要。
  • Make it easy for the reader to understand what you are doing, and even more importantly, why.让读者很容易理解你在做什么,更重要的是,为什么。
  • A good comment in the right place can save a week of headaches.在正确的地方发表好的评论可以节省一周的头痛。
  • The reader might be you.读者可能是你。
  • Always try to choose the best tool for the job.始终尝试为工作选择最佳工具。
  • Follow conventions where they make sense, but do not hesitate to break the rules when you have a justified reason for it.在有意义的地方遵循约定,但当你有正当理由时,不要犹豫打破规则。
  • Nobody knows better what makes sense than the person writing the code.没有人比编写代码的人更清楚什么是有意义的。 Keep asking yourself: Does the thing that I'm doing make sense, or do I see a better way?不断问自己:我所做的事情是否有意义,或者我是否看到了更好的方法?
  • Less is more.少即是多。

The long short of it is yes listen to the feedback.长话短说是听反馈。 While true that Python does not really do privacy the same as C++ or Java, it still sends signals to developers and users what the intention is.虽然 Python 确实没有像 C++ 或 Java 那样真正做到隐私,但它仍然向开发人员和用户发出信号,表明其意图是什么。 public attributes can be set any which way, and this can lead to an error if you always expect "object.x" to be an integer and they make it a string.公共属性可以以任何方式设置,如果您总是期望“object.x”是 integer 并且将其设为字符串,这可能会导致错误。 when an attribute is made private (while not truly private) means the user cannot access it directly, and must user a provided handler.当一个属性被设为私有时(虽然不是真正的私有)意味着用户不能直接访问它,并且必须使用提供的处理程序。 The handler methods provide the only means of access and require all interaction to use the same channel.处理程序方法提供了唯一的访问方式,并要求所有交互都使用相同的通道。 The same idea also applies to methods.同样的想法也适用于方法。 there are some methods the user/developer should never need to use directly, and these should be private for the sake of simplifying the encapsulation.有些方法是用户/开发人员永远不需要直接使用的,为了简化封装,这些方法应该是私有的。 While not entirely necessary, using Python's privacy features does aid encapsulation and helps prevents unforeseen errors from occurring.虽然并非完全必要,但使用 Python 的隐私功能确实有助于封装并有助于防止发生不可预见的错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM