知行编程网知行编程网  2022-12-02 00:30 知行编程网 隐藏边栏  8 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于Python3中_和__的用途和区别的相关知识,包括功能和功效最大的区别,以及价值和作用的区别这些编程知识,希望对大家有参考作用。

Python3中_和__的作用和区别

在查看一些 Python 开源代码时,你经常会看到以下划线或双下划线开头的方法或属性。它们的作用是什么,有什么区别?今天总结一下(注:以下代码是在Python3下测试的)


_ 的含义

python 类中没有真正的私有属性或方法,没有真正的私有化。

但是出于编程的需要,我们往往需要区分私有方法和公共方法,以方便管理和调用。那么如何在 Python 中实现呢?

一般情况下,Python 同意带_下划线的属性和方法是私有方法或属性,表示这些属性和方法不应该被外部调用,也不会被from ModuleA import * 导入。调用不会报错,但不符合规范。

以下代码演示了带有 _ 的方法及其在类外部的可访问性。

class TestA:
    def _method(self):
        print('I am a private function.')
    def method(self):
        return self._method()
ca = TestA()
ca.method()

输出

I am a private function.


__ 的含义

Python中的__与一种叫做name mangling的技术有关,name mangling(也称为名称修饰)。在许多现代编程语言中,这种技术用于解决因需要唯一名称而引起的问题,例如命名冲突/重载等。

Python中的双下划线是为了防止子类重写属性方法。类实例化时自动转换,在类中双下划线开头的属性方法前加上“_类名”。

class TestA:
    def __method(self):
        print('This is a method from class TestA')
    def method(self):
        return self.__method()
class TestB(TestA):
    def __method(self):
        print('This is a method from calss TestB')
ca = TestA()
cb = TestB()
ca.method()
cb.method()

输出结果

This is a method from class TestA
This is a method from class TestB

在类TestA中,由于name mangling技术,__method方法实际上会自动转换为_TestA__method,所以A中的method方法返回_TestA__method,而TestB是TestA的子类,只是重写了__method方法,而method方法不是重写,所以调用B中的method方法时,还是调用了_TestA__method方法。

注意:A中没有__method方法,只有_A__method方法,外面也可以直接调用,所以python中没有真正的私有化

不能直接调用__method()方法, 需要调用转换之后的方法

ca.__method()

输出

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'TestA' object has no attribute '__method'

转换后的方法名为:_TestA__method

ca._TestA__method()


输出

This is a method from class TestA

在TestB中重写method方法:

class TestB(TestA):
    def __method(self):
        print('This is a method from calss TestB')
    def method(self):
        return self.__method()
cb = B()
cb.method()


输出

This is a method from calss TestB

现在TestB中的method方法会调用_TestB__method方法。

总结

python中没有真正的私有化,但是有一些命名相关的约定让程序员处理一些需要私有化的情况。

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享