diff --git a/新建文本文档 (3).txt b/新建文本文档 (3).txt new file mode 100644 index 0000000..5bd3be5 --- /dev/null +++ b/新建文本文档 (3).txt @@ -0,0 +1,61 @@ +(1)参数的类型取决于它们的值,而不是使用它们的函数签名。 您可以从命令行传递任何Python文本:数字,字符串,元组,列表,字典(仅在某些版本的Python中支持集合)。只要它们只包含文字,您也可以任意嵌套这些集合。 + +为了演示这个,我们将制作一个小程序,通过这个小程序告诉我们传给它的参数类型: + +import fire +fire.Fire(lambda obj: type(obj).__name__) + +我们会像这样使用它: + +$ python example.py 10 +int +$ python example.py 10.0 +float +$ python example.py hello +str +$ python example.py '(1,2)' +tuple +$ python example.py [1,2] +list +$ python example.py True +bool +$ python example.py {name: David} +dict +在最后一个例子中,你会注意到裸词会自动替换为字符串。 +要注意! 如果你想传递字符串"10"而不是int 10,你需要转义或者引用你的引号。 否则,Bash将会把你的引用取消并将一个没有引号的10传递给你的Python程序,在那里Fire将把它解释为一个数字。 + +$ python example.py 10 +int +$ python example.py "10" +int +$ python example.py '"10"' +str +$ python example.py "'10'" +str +$ python example.py \"10\" +str +要注意! 记住Bash首先处理你的参数,然后Fire解析结果。 如果你想将dict {"name":"David Bieber"}传递给你的程序,你可以试试这个: +$ python example.py '{"name": "David Bieber"}' # Good! Do this. +dict +$ python example.py {"name":'"David Bieber"'} # Okay. +dict +$ python example.py {"name":"David Bieber"} # Wrong. This is parsed as a string. +str +$ python example.py {"name": "David Bieber"} # Wrong. This isn't even treated as a single argument. + +$ python example.py '{"name": "Justin Bieber"}' # Wrong. This is not the Bieber you're looking for. (The syntax is fine though :)) +dict +(2)rue和False被解析为布尔值。 + +你也可以通过--flag语法--name和--noname来指定布尔值,它们分别将名称设置为True和False。 + +继续前面的例子,我们可以运行以下任何一个: + +$ python example.py --obj=True +bool +$ python example.py --obj=False +bool +$ python example.py --obj +bool +$ python example.py --noobj +bool