[英]How to type-hint non-basic parameters of a function?
In python3.6在python3.6
Suppose you have a function that has 5 arguments.假设您有一个具有 5 个 arguments 的 function。 A a string object, a list of string objects,
argparse.Namespace
object, a pandas dataframe, and a logging.Logger
object. A a string object, a list of string objects,
argparse.Namespace
object, a pandas dataframe, and a logging.Logger
object.
Such as如
def main(a_string, a_list, args, df, logger):
The first two are pretty straight forward, but how do you cast the last 2?前两个很简单,但你如何投射最后两个?
def main(a_string: str, a_list: List[str], args: ???, df: ???, logger: ???) -> str:
Thanks in advance!提前致谢!
for reference:
type(args) -> argparse.Namespace
type(logger) -> logging.Logger
If I understand your question correctly, it's the same as for str
(a type), ie enter the type:如果我正确理解了您的问题,则与
str
(类型)相同,即输入类型:
def main(a_string: str, a_list: List[str], args: argparse.Namespace df: pandas.DataFrame, logger: logging.Logger) -> str:
Though I generally refactor to avoid long arg lists.虽然我通常会重构以避免长 arg 列表。 I'm not a fan of having line breaks in my function signatures.
我不喜欢在我的 function 签名中出现换行符。
If you need to find the type(s) of an object:如果您需要查找 object 的类型:
>>> type(logging.root)
logging.RootLogger
>>> type(logging.root).__bases__
(logging.Logger,)
As Kache typed earlier, that should be the right answer, but you must be concerned of two things when doing that stuff:正如 Kache 之前键入的那样,这应该是正确的答案,但是在做这些事情时你必须关注两件事:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.