python封装和解构

导文

关于封装和解构的问题,需要对官方文档了解深透时,才能写出更为完美的语言。

封装

封装是将默认一些操作封装为某种数据格式,到底封装为哪种类型的数据格式呢?通过如下实验验证:

#!/bin/python3
#-*- coding: UTF-8 -*-
t0 = (10,20)
t1 = 10,20
x,y = (1,2)
print(type(t0))
print(type(t1))
print("{}+{}={}".format(x,y,x+y))
x,y = t1
print("{}+{}={}".format(x,y,x+y))
运行结果如下:
<class 'tuple'>
<class 'tuple'>
1+2=3
10+20=30

封装说明

封装:

  • 将多个值使用逗号分割,组合在一起
  • 本质上返回一个元组,只是省略了小括号
  • python特有语法,被很多语言学习和借鉴

解构

abstract[可能的任意的]

在PEP 448的文档中第一句话就是这样的:This PEP proposes extended usages of the * iterable unpacking operator and ** dictionary unpacking operators to allow unpacking in more positions, an arbitrary number of times, and in additional circumstances. Specifically, in function calls, in comprehensions and generator expressions, and in displays.
就是说:这个PEP提出建议扩展使用解包可迭代对象和字典的操作,此操作可以解构更多的位置,任意次及在其他情况下,通常可以用在函数调用,理解,生成器表达式及显示中。

#!/bin/python3
#-*- coding: UTF-8 -*-
#function calls are proposed to support an arbirary number times
print(*[1],*[2],3)
print(dict(**{"x":1},y=2,**{"z":3}))
#unpacking is proposed to be alled inside tuple,list
#packing tuple commal逗号
a = *range(4),4 
print(a)
#list
b = [*range(4),4]
print(b)
#set
c = {*range(4),4}
print(c)
#dict
d = {"x":1,**{"y":2,"z":3}}
print(d)
运行结果:
1 2 3
{'x': 1, 'y': 2, 'z': 3}
(0, 1, 2, 3, 4)
[0, 1, 2, 3, 4]
{0, 1, 2, 3, 4}
{'x': 1, 'y': 2, 'z': 3}

rationale[说明]

Current usage of the * iterable unpacking operator features unnecessary restrictions that can harm readability.
当前使用解构可迭代对象的特性会造成易读性的一些损坏,
Unpacking multiple times has an obvious rationale. When you want to unpack several iterables into a function definition or follow an unpack with more positional arguments, the most natural way would be to write:
当解构多次时有一个很明显的说明,当你想要解构一些可迭代对象到函数或者更多的参数是,最自然的写法应该是:
function(**kw_arguments, **more_arguments)
function(*arguments, argument)
Simple examples where this is useful are print and str.format. Instead, you could be forced to write:

kwargs = dict(kw_arguments)
kwargs.update(more_arguments)
function(**kwargs)

args = list(arguments)
args.append(arg)
function(*args)

两大用法

对称性赋值

对称性赋值在很多情况下被称为解构,其实只是解构的一小部分

  • 线性结构(包括字典和list等)的元素解开,并顺序的赋值给其他变量
  • 左边接纳的变量数要和右边解开的元素个数一致
    原文描述如下(官方叫对称性赋值,本质是解构顺序赋值):
    Firstly there is a symmetry of assignment, where fst, *other, lst = elems and elems = fst, *other, lst are approximate inverses, ignoring the specifics of types. This, in effect, simplifies the language by removing special cases.
    通常用在x,y = y,x 相当于将y,x先封装为一个元组(y,x),等价于x,y = (y,x),然后依据位置参数进行依次赋值。
简化添加类型

Secondly, it vastly simplifies types of “addition” such as combining dictionaries, and does so in an unambiguous and well-defined way:
其次,它极大地简化了“添加”的类型,比如结合词典,并且以明确和明确的方式进行:

combination = first_dirctionary.copy()
combination.update({"x":1,"y":2})
等价于:
combination ={**first_dictionary,"x":1,"y":2}

实验总结

实验证明,证明解构的abstract、rationale、对称性和简化操作:

#!/bin/python3
#-*- coding: UTF-8 -*-
a,b = 1,2
print(a,b)
a,b =(1,2)
print(a,b)
a,b = [1,2]
print(a,b)
a,b = {1,2}
print(a,b)
a,b = {'a':1,'b':2}
print(a,b)
#a,b = {1,2,3}
#print(a,b)
#相当于a接受1,*b=[2,3]
a,*b = {1,2,3}
print(a,b)
#对称性+abstract,对称性是左右两边的数字得一致,
# *表示abstract任意多的和rationale理论基础
a,*b,c = {1,2,3,4,5,6,7,8}
print(a,b,c)
运行结果:
1 2
1 2
1 2
1 2
a b
1 [2, 3]
1 [2, 3, 4, 5, 6, 7] 8
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值