为什么python没有访问修饰符?python有什么替代方法? [英] why python does not have access modifier?And what are there alternatives in python?

查看:203
本文介绍了为什么python没有访问修饰符?python有什么替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么python不像c#,java(即public,private等)那样具有Access修饰符?python中封装和信息隐藏的替代方法是什么。

why python does not have Access modifier like in c#, java i.e public, private etc.what are the alternative way of encapsulation and information hiding in python.

推荐答案

Wikipedia


[Python]使用名称处理对私有变量的支持有限。有关详细信息,请参见本教程的类部分。不过,许多
Python用户并不需要私有变量。
标语我们都同意这里的成年人 用来形容这笔
的态度。有些人认为
中的信息隐藏是非Python的,它表明所涉及的类包含不美观的或
计划不良的内部结构。但是,名称
改写的最有力论据是防止程序意外损坏:如果
在超类中引入新的公共变量,则如果子类
不使用私有变量,则它们可能会中断。

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

从教程中开始:
对于模块来说,确实是
,Python中的类不会在
之间设置绝对障碍定义和用户,而是依靠用户的礼貌而不是
来闯入定义。

我们都一样 The Hitchhiker's Python指南!

这篇关于为什么python没有访问修饰符?python有什么替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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