python如果语句用字符串为条件

发布于 2025-02-12 01:56:19 字数 674 浏览 1 评论 0 原文

virtualenv 中有一些代码,这些代码正在绊倒我。 It's this:

path.decode("utf-8") if "__DECODE_PATH__" else path

from

。它可能是一个错误,但是由于它在 virtualenv 中,并且由于 activate_this.py 是当前解释器中激活 virtualenv 的手段,因此似乎不太可能。

另外,还有一个问题有人提到有人遇到这个问题,但是它得到了解决。没有人提到如果“ __decode_path __” 看不正确。

最终,这意味着我在尝试使用 activate_this.py 时,我获得 attributeError:'str'对象没有属性'decode'。但是,如果我可以说服代码沿 else 路径下去,则可以正常工作。

注意:在Linux Mint 20.3中使用Python 3.8.10

There's some code in virtualenv that's tripping me up. It's this:

path.decode("utf-8") if "__DECODE_PATH__" else path

from activate_this.py line 28.

How can if "__DECODE_PATH__" ever be false? It's possible it's a bug, but since it's in virtualenv and since activate_this.py is the means of activating virtualenv in the current interpreter, it's seems unlikely.

Also there is an issue where someone mentions having this problem but it then being resolved. No one mentions if "__DECODE_PATH__" looking incorrect.

Ultimately it means I get AttributeError: 'str' object has no attribute 'decode' when I try to use activate_this.py. However if I could convince the code to go down the else path it would work fine.

Note: using Python 3.8.10 in Linux Mint 20.3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2025-02-19 01:56:19

从代码中,看来文件只是一个模板,其中包含一些替换的魔术字符串。换句话说,它们只是模板变量。

您可以在 基类。

然后在 类,您可以看到“ __ decode_path __” 是这些魔术字符串之一。

From the code, it appears that file is just a template which contains some magic strings that are replaced. In other words, they are just template variables.

You can see the replacement in action in the ViaTemplateActivator base class.

Then in the PythonActivator class you can see that "__DECODE_PATH__" is one of those magic strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文