栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 语言在实际使用中遇到的一些缺点和不足

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 语言在实际使用中遇到的一些缺点和不足

综合影响范围和影响程度降序排列.

TypedDict 在描述较复杂的嵌套字典时表现乏力 | typehint

主要表现为:

  • 书写顺序混乱
  • (pycharm) 对值的类型判断存在问题, 特别是处理后向引用时
  • 结构可读性差

TODO:提供代码参考

open 函数的 encoding 默认值不是 ‘utf-8’ | builtins; open

略.

Callable 对函数的私有属性无法识别 | typehint
from typing import Callable

def xxx(func: Callable) -> None:
    _ = func.__code__.co_argcount
#           ~~~~~~~~~~ cannot find reference '__code__' in '(...) -> Any'!

相关阅读:

  • https://stackoverflow.com/questions/37835179/how-can-i-specify-the-function-type-in-my-type-hints
Iterable, Sized 二者不可得兼? | typehint
import typing as t

def some_func(a: t.Iterable[str]):
    for i in a: print(i)
    print(len(a))
#         ~~~~~~ expected type "Sized", got "Iterable[str]" instead!
import typing as t

def some_func(a: t.Sized):
    for i in a: print(i)
#   ~~~~~~~~~~ expected type "Iterable[str]", got "Sized" instead!
    print(len(a))

已解决: 使用 typing.Sequence.

相关阅读: https://stackoverflow.com/questions/49427944/typehints-for-sized-iterable-in-python

windows 版 pip 在挂 vpn 的情况下使用 pip install 出现 ssl 认证失败 | pip; urllib3

经观察, 是因为 ~/site-packages/pip/_vendor/urllib3 的版本引发的问题:

urllib3 v1.26+ 会出现此问题, 降级到 v1.25.9 可解决.

返回 tuple 时, 如果 tuple 只有一个元素, 写法上不明显, 可能看漏掉 | syntax
def foo() -> tuple[str, ...]:
    return 'hello world',
    #                   ^ 不够明显, 有时候会看漏.
对 *args 和 **kwargs 进行类型注解存在限制 | typehint

TODO:Description

self 无法在海象运算符中使用 | 海象运算符
class AAA:
    def xxx(self):
        a = {
            'aaa': (self.value := True),
            #       ~~~~ unresolved reference: 'self'!
        }
重写 shelve 关闭方法, 导致内建函数 open 出现异常 | open

复现代码:

import shelve

class MyShelve(shelve.Shelve):

    def close(self):
        super().close()

        with open('result.txt', 'w') as f:
        #    ~~~~ NameError: name 'open' is not defined!
            f.write('program finished at {time}.'.format(time=...))

pycharm 相关的问题 pycharm 提示功能在显示 open 函数时严重影响体验

如下图, 键入 open() 后给出的提示完全遮住了编辑界面:

pycharm 对 typing.TypedDict 的实例化写法支持度非常差

TODO:Description

使用使用到海象运算符和字典取值, 可能出现一个奇怪的弱警告
a = {'a': 1, 'b': 2, 'c': 3}
if (x := a['c']) and x > 1:
#         ^ 在这个位置偶尔会出现一个让人无法理解的弱警告: UnresolvedReference.
    print(x)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号