-3 python variables loops amazon-web-services boto3
当我执行此代码时,它返回 None,但我想像循环中那样打印整数,有人知道为什么吗?
import boto3
client = boto3.client('autoscaling')
clearesponse = client.describe_auto_scaling_groups(
AutoScalingGroupNames=[
'XXXX',
])
for reservation in clearesponse ['AutoScalingGroups']:
test=print(reservation['DesiredCapacity'])
nb_desired_cap = test
print (nb_desired_cap)
print (test)
Run Code Online (Sandbox Code Playgroud)
跑步 :
python 描述实例.py
4
没有任何
没有任何
运行代码并希望在其他变量中捕获结果 4 以用于代码的其他部分
这是因为该print函数返回None. 在下面的示例中,“Hello, World!” 没有返回,只是打印到屏幕上,返回的是None.
>>> result = print("Hello, World!")
Hello, World!
>>> result is None
True
Run Code Online (Sandbox Code Playgroud)
您可以将此与不打印任何内容、仅返回输入的函数进行对比。如果您在脚本中使用它,它不会在屏幕上打印任何内容。在 REPL 中,这不是不言而喻的,但对于字符串,您可以注意到引号。
>>> def identity(x):
... return x
...
>>> result = identity("Hello, World!")
>>> result == "Hello, World!"
True
Run Code Online (Sandbox Code Playgroud)
最后,您可以同时执行以下两项操作:
>>> def myprint(string):
... print(string)
... return string
...
>>> myprint("Hello, World!")
Hello, World!
'Hello, World!'
Run Code Online (Sandbox Code Playgroud)
如果您在 REPL 中运行上面的函数,它会显示输入两次,而在脚本中它只会显示一次(打印值)。
我不确定您希望代码执行什么操作,但您可能希望将 . 返回的结果以外的内容分配给变量print。
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |