Python学习之私有函数,私有变量及封装详解

 更新时间:2022年03月10日 08:49:07   作者:渴望力量的哈士奇  
私有函数与私有变量中的私有就是独自拥有、不公开、不分享的意思。放到函数与变量中就是独自拥有的函数与独自拥有的变量,并且不公开。本文将通过示例详细讲解Python中的私有函数、私有变量及封装,感兴趣的可以学习一下

通过学习私有函数与私有变量,可以更好的完善 类的开发 ,从而丰满我们的场景与实现方案。

什么是私有函数和私有变量

私有函数与私有变量中的私有是什么意思? —> 简单理解就是独自拥有、不公开、不分享的意思。放到函数与变量中就是独自拥有的函数与独自拥有的变量,并且不公开。这样我们就理解了什么是私有函数与私有变量。

  • 无法被实例化后的对象调用的类中的函数与变量
  • 虽然无法被实例化后的对象调用,但是在 类的内部我们可以 调用私有函数与私有变量
  • 私有函数与私有变量的目的:只希望类内部的业务调用使用,不希望被实例化对象调用使用
  • 既然有私有函数与私有变量,其实能被实例化对象调用的函数与变量就是公有函数与公有变量,不过一般我们都称之为函数与变量。

私有函数与私有变量的定义方法

如何定义私有函数与私有变量:在 类变量 与 类函数 前添加 __ (2个下横线)即可定义私有函数与私有变量;变量或函数的后面无需添加,左右都有两个下横线,是 类的内置函数 的定义规范。

私有函数与私有变量示例如下:

class Persion(object):
    
    def __init__(self):
        self.name = name
        self.__age = 18                    # 'self.__age' 为 Persion类 私有变量
        
    def run(self):
        print(self.name, self.__age)    # 在  Persion类 的代码块中,私有变量依然可以被调用
        
    def __eat(self):                    # '__eat(self)' 为 Persion类 私有函数
        return 'I want eat some fruits'    

接下来我们根据上面的示例代码做一下修改,更好的演示一下 私有函数与私有变量 方便加深理解

class PersionInfo(object):

    def __init__(self, name):
        self.name = name

    def eat(self):
        result = self.__eat()
        print(result)

    def __eat(self):
        return f'{self.name} 最喜欢吃水果是 \'榴莲\' 和 \'番石榴\''

    def run(self):
        result = self.__run()
        print(result)

    def __run(self):
        return f'{self.name} 最喜欢的健身方式是 \'跑步\' 和 \'游泳\''


persion = PersionInfo(name='Neo')
persion.eat()
persion.run()

# >>> 执行结果如下:
# >>> Neo 最喜欢吃水果是 '榴莲' 和 '番石榴'
# >>> Neo 最喜欢的健身方式是 '跑步' 和 '游泳'

我们再试一下 通过 实例化对象 persion 调用 __eat 私有函数试试

class PersionInfo(object):

    def __init__(self, name):
        self.name = name

    def eat(self):
        result = self.__eat()
        print(result)

    def __eat(self):
        return f'{self.name} 最喜欢吃水果是 \'榴莲\' 和 \'番石榴\''

    def run(self):
        result = self.__run()
        print(result)

    def __run(self):
        return f'{self.name} 最喜欢的健身方式是 \'跑步\' 和 \'游泳\''


persion = PersionInfo(name='Neo')
persion.__eat()

# >>> 执行结果如下:
# >>> AttributeError: 'PersionInfo' object has no attribute '__eat'
# >>> 再一次证明 实例化对象是不可以调用私有函数的

那么事实真的是 实例化对象就没有办法调用 私有函数 了么?其实不是的,我们继续往下看

class PersionInfo(object):

    def __init__(self, name):
        self.name = name

    def eat(self):
        result = self.__eat()
        print(result)

    def __eat(self):
        return f'{self.name} 最喜欢吃水果是 \'榴莲\' 和 \'番石榴\''

    def run(self):
        result = self.__run()
        print(result)

    def __run(self):
        return f'{self.name} 最喜欢的健身方式是 \'跑步\' 和 \'游泳\''


persion = PersionInfo(name='Neo')

# 通过 dir() 函数 查看一下 实例化对象 persion 中都有哪些函数?

print(dir(persion))

可以看到 实例化对象 persion 也有两个私有变量 _Persion__eat 和 _Persion__run ,尝试直接用实例化对象 persion 调用私有变量。

class PersionInfo(object):

    def __init__(self, name):
        self.name = name

    def eat(self):
        result = self.__eat()
        print(result)

    def __eat(self):
        return f'{self.name} 最喜欢吃水果是 \'榴莲\' 和 \'番石榴\''

    def run(self):
        result = self.__run()
        print(result)

    def __run(self):
        return f'{self.name} 最喜欢的健身方式是 \'跑步\' 和 \'游泳\''


persion = PersionInfo(name='Neo')

# 通过 dir() 函数 查看一下 实例化对象 persion 中都有哪些函数?

print(dir(persion))

print(persion._PersionInfo__eat())
print(persion._PersionInfo__run())

# >>> 执行结果如下图:

可以看到通过这种方式,我们的 实例化对象 persion 也成功的调用了 PersionInfo 类 的私有函数;但是既然是 私有函数 ,那么目的就是不希望被实例化对象调用,所以我们还是按照编码规范来使用比较好。

附:私有变量(私有属性)的使用与私有函数一样,我们看下面的示例

class PersionInfo(object):
    __car = 'BMW'

    def __init__(self, name, sex):
        self.name = name
        self.__sex = sex

    def info(self):
        result = self.__info()
        print(result)

    def __info(self):
        return f'{self.name} 性别:{self.__sex} ,他有一辆:\'{self.__car}\''


persion = PersionInfo(name='Neo', sex='男')
persion.info()

# >>> 执行结果如下:
# >>> Neo 性别:男 ,他有一辆:'BMW'


# >>> 尝试调用私有函数		私有函数与变量(属性)['_PersionInfo_01__car', '_PersionInfo_01__info', '_PersionInfo_01__sex']
print(persion01._PersionInfo_01__info())		

# >>> 执行结果如下:
# >>> Neo 性别:男 ,他有一辆:'BMW'

Python 中的封装

其实 Python 中并没有 封装 这个功能,而封装只是针对 Python 中某种业务场景的一种概念而已。

封装的概念 —> 将不对外的私有属性或方法通过可以对外使用的函数而使用(类中定义的私有函数、私有方法只能在类的内部使用,外部无法访问),这样做的主要原因是:保护隐私,明确的区分内与外。

封装的示例如下:

class Persion(object):
    
    def __hello(self, data):
        print('hello %s' % data)
        
    def helloworld(self):
        self.__hello('world')
        
        
if __name__ == '__main__'
	persion = Persion()
    persion.helloworld()
    
# >>> 执行结果如下:
# >>> hello world

# >>> 我们可以看到 helloworld() 是基于 私有函数 __hello() 来执行的;
# >>> 所以我们是通过对外的函数 helloworld() 调用了内部私有函数 __hello   ;  这就是 Python 中的 封装的概念。

面向对象编程小练习

需求:

用类和对象实现银行账户的资金交易管理,包括存款、取款和打印交易详情,交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余额。

脚本示例如下:

#coding: utf-8
import time

class MoneyExchange(object):
    money = 0
    abstract = []
    single_bill_list = []
    bill_list =[]
    transcation_num = 0
    currency_type = "人民币"
    service_option_num = []
    service_option = []
    service_menu ={
        1: "1:存款",
        2: "2:取款",
        3: "3:查看明细",
        4: "4:查看余额",
        0: "0:退出系统"
    }
    for key, value in service_menu.items():
        service_option_num.append(key)
        service_option.append(value)

    def welcome_menu(self):
        print('*' * 20 + '欢迎使用资金交易管理系统' + '*' * 20)
        for i in range(0,len(self.service_option)):
            print(self.service_option[i])
        print('*' * 60)

    def save_money(self):
        self.money_to_be_save = float(input('请输入存钱金额:'))
        self.abstract = '转入'
        self.time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        self.money += self.money_to_be_save
        self.single_bill_list.append(self.time)
        self.single_bill_list.append(self.abstract)
        self.single_bill_list.append(self.money_to_be_save)
        self.single_bill_list.append(self.currency_type)
        self.single_bill_list.append(self.money)
        self.bill_list.append(self.single_bill_list)
        self.single_bill_list = []
        self.transcation_num += 1
        print('已成功存入!当前余额为:%s 元' % self.money)
        input('请点击任意键以继续...')

    def withdraw_money(self):
        self.money_to_be_withdraw = float(input('请输入取出金额:'))
        if self.money_to_be_withdraw <= self.money:
            self.abstract = '取出'
            self.time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            self.money -= self.money_to_be_withdraw
            self.single_bill_list.append(self.time)
            self.single_bill_list.append(self.abstract)
            self.single_bill_list.append(self.money_to_be_withdraw)
            self.single_bill_list.append(self.currency_type)
            self.single_bill_list.append(self.money)
            self.bill_list.append(self.single_bill_list)
            self.single_bill_list = []
            self.transcation_num += 1
            print('已成功取出!当前余额为:%s 元' % self.money)
            input('请点击任意键以继续...')
        else:
            print('您输入的取出金额超过余额,无法操作!请重新输入')
            input('请点击任意键以继续...')

    def check_bill_list(self):
        print('|      交易日期      |  摘要  |  金额  |  币种  |  余额  |')
        for i in range(0, self.transcation_num):
            print("|%s | %s | %s | %s | %s|" % (
                self.bill_list[i][0],
                self.bill_list[i][1],
                self.bill_list[i][2],
                self.bill_list[i][3],
                self.bill_list[i][4]
            ))
        input('请点击任意键以继续...')

    def check_money(self):
        print('账户余额为:%s元' % self.money)
        input('请点击任意键以继续...')

    def user_input(self):
        option = float(input('请输入选项:'))
        if option in self.service_option_num:
            if option == 1:
                self.save_money()
            if option == 2:
                self.withdraw_money()
            if option == 3:
                self.check_bill_list()
            if option == 4:
                self.check_money()
            if option == 0:
                print('您已成功退出,谢谢!')
                exit()
        else:
            print('抱歉,你输入有误,请重新输入!')
            input('请点击任意键以继续...')

            
money_exchange = MoneyExchange()
while True:
    money_exchange.welcome_menu()
    money_exchange.user_input()
    
# >>> 执行结果如下:
# >>> ********************欢迎使用资金交易管理系统********************
# >>> 1:申请存款
# >>> 2:申请取款
# >>> 3:查看明细
# >>> 4:查看余额
# >>> 0:退出系统
# >>> ************************************************************

# >>> 根据提示执行对应的操作

PS:这个脚本并不完善,但是太晚了不想改了。

以上就是Python学习之私有函数,私有变量及封装详解的详细内容,更多关于Python私有函数 变量 封装的资料请关注脚本之家其它相关文章!

相关文章

  • python pandas query的使用方法

    python pandas query的使用方法

    这篇文章主要介绍了python pandas query的使用方法,文章通过围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-09-09
  • python生成圆形图片的方法

    python生成圆形图片的方法

    这篇文章主要为大家详细介绍了python生成圆形图片的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Python实现GIF图倒放

    Python实现GIF图倒放

    这篇文章主要介绍了Python如何实现GIF图倒放,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • Python 中将值附加到集合的操作方法

    Python 中将值附加到集合的操作方法

    这篇文章主要介绍了Python 中将值附加到集合的操作方法,通过使用 add() 方法或 update() 方法,你可以向 Python 中的集合中添加元素,在添加元素时,需要注意不允许重复元素和集合是无序的,本文通过示例代码给大家介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • MySQL适配器PyMySQL详解

    MySQL适配器PyMySQL详解

    这篇文章主要为大家详细介绍了MySQL适配器PyMySQL的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • sklearn-SVC实现与类参数详解

    sklearn-SVC实现与类参数详解

    今天小编就为大家分享一篇sklearn-SVC实现与类参数详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python中import和from-import的区别解析

    python中import和from-import的区别解析

    这篇文章主要介绍了python中import和from-import的区别解析,本文通过实例代码给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • Python numpy.transpose使用详解

    Python numpy.transpose使用详解

    本文主要介绍了Python numpy.transpose使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Python使用os模块实现更高效地读写文件

    Python使用os模块实现更高效地读写文件

    os是python标准库,包含几百个函数常用路径操作、进程管理、环境参数等好多类。本文将使用os模块实现更高效地读写文件,感兴趣的可以学习一下
    2022-07-07
  • Python批量删除txt文本指定行的思路与代码

    Python批量删除txt文本指定行的思路与代码

    在深度学习项目中常常会处理各种数据集,下面这篇文章主要给大家介绍了关于Python批量删除txt文本指定行的思路与代码,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-02-02

最新评论