小编Pro*_*o Q的帖子

如何在python中重命名超类的方法?

我有一个超类使用该方法run().

我创建了一个超类的子类,我想拥有自己的run()方法.但是,我想在一个oldrun()在这个新对象上调用的方法中保留旧run方法的功能.

我将如何在Python中执行此操作?

python methods class subclass superclass

3
推荐指数
1
解决办法
562
查看次数

Getting a context manager created with @contextmanager to work properly with exceptions

I have the following code

from contextlib import contextmanager

@contextmanager
def simple_context_manager():
    print("starting context manager")
    yield
    print("finished context manager")

try:
    with simple_context_manager():
        raise RuntimeError
except RuntimeError:
    print("Caught the error")
print("Moving on")
Run Code Online (Sandbox Code Playgroud)

Right now it prints out

starting context manager
Caught the error
Moving on
Run Code Online (Sandbox Code Playgroud)

which tells me that the context manager isn't closing. How can I get it to close and print the "finished context manager" line?

Since I'm using the decorator, I don't have a dedicated __exit__ function that …

python error-handling contextmanager python-3.x

3
推荐指数
1
解决办法
111
查看次数

什么是NotImplemented?

我有以下功能:

   def __eq__(self, other: object) -> Union[bool, NotImplemented]:
        try:
            for attr in ["name", "variable_type"]:
                if getattr(self, attr) != getattr(other, attr):
                    return False
            return True
        except AttributeError:
            return NotImplemented  # Expression has type "Any"
Run Code Online (Sandbox Code Playgroud)

我正在运行mypy,它告诉我NotImplemented的类型为“ Any”,这显然是不希望的。

有什么更好的类型可以用来消除此错误?还是我只需要插手type: ignore并继续前进?(此外,我NotImplemented在返回类型中使用的是正确的吗?)

python python-3.x

3
推荐指数
1
解决办法
80
查看次数

如何使用 Pillow 将动画 GIF 保存到变量

我从这里发现我可以使用 Pillow 创建和保存动画 GIF。但是,该save方法似乎没有返回任何值。

我可以将 GIF 保存到文件中,然后使用 打开该文件Image.open,但这似乎没有必要,因为我并不真正希望保存 GIF。

如何将 GIF 保存到变量而不是文件中?也就是说,我希望能够制作some_variable.show()并显示 GIF,而无需将 GIF 保存到我的计算机上。

variables gif save python-imaging-library

2
推荐指数
1
解决办法
3138
查看次数

如何获得格式而不导致类型提示错误?

我在 Python 中有以下列表推导式:

from typing import cast

# everything is fine
print([value for value in [1, 2, 3, 4]])

# on the first "value": Expression type contains "Any" (has type "List[Any]")
print("{}".format([value for value in [1, 2, 3, 4]]))

# on the "cast": Expression type contains "Any" (has type "List[Any]")
print("{}".format([cast(int, value) for value in [1, 2, 3, 4]]))
Run Code Online (Sandbox Code Playgroud)

为什么使用format会导致 Mypy 返回错误?如您所见,我尝试使用强制转换,但仍然失败。

这个问题看起来很相似,但我的特殊情况很奇怪,因为只要我不使用该format函数,Mypy 似乎就可以了(但该函数始终没问题print)。

有什么我可以做的,不让带格式的行给我错误?(或者我应该只是# type: ignore他们?)

编辑:请注意,这似乎不仅仅是我的 Atom linter 的问题。我使用的是 …

python list-comprehension type-hinting python-3.x mypy

2
推荐指数
1
解决办法
549
查看次数

如何输入常量并集

我想做以下事情:

from typing import Union, Literal
YES = 1
NO = 0
ValidResponse = Union[Literal[YES], Literal[NO]]
Run Code Online (Sandbox Code Playgroud)

当然,Python 不会让我这样做,因为YESNO被认为是变量,而不是文字数字。

显然我可以做到ValidResponse = Union[Literal[1], Literal[0]],但作为稍后编辑此文件的程序员,为了更改YES使用的常量(例如YES = 'yes'),我需要在两个不同的地方更改它,这似乎很奇怪。

处理这种情况的最佳方法是什么?

python type-hinting python-typing

1
推荐指数
1
解决办法
1430
查看次数