这篇“Python私有函数,私有变量及封装的方法”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“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 中某种业务场景的一种概念而已。
封装的概念 —> 将不对外的私有属性或方法通过可以对外使用的函数而使用(类中定义的私有函数、私有方法只能在类的内部使用,外部无法访问),这样做的主要原因是:保护隐私,明确的区分内与外。
封装的示例如下:
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:退出系统
# >>> ************************************************************
# >>> 根据提示执行对应的操作
以上就是关于“Python私有函数,私有变量及封装的方法”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。