gpt4 book ai didi

python - 为什么 python 没有访问修饰符?python 中有哪些替代方法?

转载 作者:太空狗 更新时间:2023-10-29 18:22:13 24 4
gpt4 key购买 nike

为什么 python 没有像 c#、java 中那样的访问修饰符,即 public、private 等。在 python 中封装和信息隐藏的替代方法是什么。

最佳答案

来自 Wikipedia :

[Python] has limited support for private variables using name mangling. See the "Classes" section of the tutorial for details. ManyPython users don't feel the need for private variables, though. Theslogan "We're all consenting adults here" is used to describe thisattitude. Some consider information hiding to be unpythonic, inthat it suggests that the class in question contains unaesthetic orill-planned internals. However, the strongest argument for namemangling is prevention of unpredictable breakage of programs:introducing a new public variable in a superclass can break subclassesif they don't use "private" variables.

From the tutorial:As is truefor modules, classes in Python do not put an absolute barrier betweendefinition and user, but rather rely on the politeness of the user notto "break into the definition."

We are all consenting adults 中描述了相同的情绪The Hitchhiker’s Guide to Python!

关于python - 为什么 python 没有访问修饰符?python 中有哪些替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22656024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com