python 3.7中对于tuple

本文介绍了从Python 2.7迁移到Python 3.7时遇到的tuple拆箱处理的不同以及map()方法返回类型的变化。在Python 3.7中,tuple参数拆箱不再支持,map()返回的是迭代器而非列表,需要通过特定方式转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

教程地址:零基础入门深度学习(1) - 感知器

在搭建感知器过程中体会到的python2.7(作者使用的版本)和python 3.7(本宝使用的版本)的区别:
  1. 首先是对于tuple的拆箱处理,如果沿用2.7下的写法会得到提示:

tuple parameter unpacking is not supported in Python 3

# python 2.7版本下的写法
lambda (x, w): x * w, zip(input_vec, self.weights)

# python 3.7版本下的写法
lambda x_w: x_w[1] + rate * delta * x_w[0], zip(input_vec, self.weights)

改动的原因,不知道,也不重要。可以参考PEP 3113 – Removal of Tuple Parameter Unpacking,官方给出的例子是:

As tuple parameters are used by lambdas because of the single expression limitation, they must also be supported. This is done by having the expected sequence argument bound to a single parameter and then indexing on that parameter:

渣渣翻译:由于lambda的单行表示限制,tuple参数必须依然能够用在lambda中。可以通过绑定单个参数然后用它们在这个参数中的索引来表示不同的变量。

# python 2.7版本下的写法
lambda (x, y): x + y

# python 3.7版本下的写法
lambda x_y: x_y[0] + x_y[1]

  2. map()方法返回的结果类型的改变
  参考 What’s New In Python 3.0,其中仅次于print()由语句输出改变为方法的,就是map()返回类型的改变。

map() and filter() return iterators. If you really need a list and the input sequences are all of equal length, a quick fix is to wrap map() in list(), e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).

If the input sequences are not of equal length, map()will stop at the termination of the shortest of the sequences. For full compatibility with map() from Python 2.x, also wrap the sequences in itertools.zip_longest().

# python 2.7版本下的写法
map(func, *sequences)
# python 3.7版本下的写法
list(map(func, itertools.zip_longest(*sequences))).

不翻译了,大概的意思就是如果你需要确切的list作为返回值的时候需要使用list()方法,特别提到了在lambda中的使用。(后面的不会翻译,不理解语境)

### Python 3.73.10 的主要差异 #### 新特性引入 Python 3.10 引入了一些新的语法特性和改进,这些变化使得代码更加简洁和易读。例如,在条件表达式中可以使用更直观的模式匹配语句[^1]。 ```python match point: case (0, 0): print("Origin") case (0, y): print(f"Y={y}") case (x, 0): print(f"X={x}") case (x, y): print(f"X={x}, Y={y}") case _: raise ValueError("Not a tuple!") ``` 而在 Python 3.7 中并没有此功能,开发者仍需依赖传统的 `if-elif` 结构来实现类似的逻辑判断。 #### 类型提示增强 自版本 3.9 开始(适用于 Python 3.10),对于内置集合类型的类型标注可以直接采用标准库中的类名而无需再导入额外模块。比如列表、字典等数据结构可以直接写作 `list[int]`, `dict[str, float]` 而不是像之前那样要写成 `List[int]` 或者 `Dict[str, float]`。 #### 错误处理机制优化 Python 3.10 对异常捕捉进行了简化,允许在一个 except 子句里捕获多个不同种类的错误并执行相同的响应操作。这减少了冗余代码量的同时提高了可维护性。 ```python try: ... except (KeyError, AttributeError) as e: handle_error(e) ``` 相比之下,Python 3.7 需要在同一个 try 块内分别定义针对每种可能发生的特定异常情况下的处理方式。 #### 向下兼容性考量 尽管存在上述区别,大多数情况下由 Python 3.7 编写的程序可以在不做任何修改的情况下运行于更高版本解释器之上;然而某些新加入的关键字可能会引起冲突如果它们被用作变量名称,则需要适当调整源码以适应最新版环境的要求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值