如何只允许 python 类型中的某些字符?

问题描述

我怎样才能在 python 中做这样的事情?

type something = "a" | "b"
const anotherthing: something = "c" // gives error

此打字稿代码不允许将 anotherthing 的值设置为 ab 以外的任何值,如果我尝试使用任何其他值,它会在 ide 中出现错误.

我怎样才能在 python 中使用这样的东西?

我已经尝试了以下 python 代码

from typing import Union

something = Union["a","b"]
anotherthing: something = "c" # it does not give any error

我使用 pycharm 作为我的 ide。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)