[英]Boto3 different outputs
我正在嘗試獲取我創建的 ec2 的實例 ID。 但是我得到 2 個不同的輸出,為什么? 請檢查打印輸出。
print (a)
i-0ae514dcs36chb154
print (instance)
ec2.Instance(id='i-0ae514dcs36chb154')
ec=ec2.create_instances(
ImageId=ami,
InstanceType=type,
MinCount=size,
MaxCount=size,
KeyName=keyname,
SecurityGroupIds=[sg],
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': name,
},
],
},
],
)
a= ec[0].id
print (a) # Output: i-0ae514dcs36chb154
instance = ec2.Instance(a)
print (instance) # Output : ec2.Instance(id='i-0ae514dcs36chb154')
謝謝。
如這里所見:
當您調用create_instances()
它返回一個ec2.Instance()
列表,它是一個具有屬性id
的對象。
寫的時候,
a = ec[0].id # Create a variable called "a" that points to the value of the "id" attribute of the first element in the list
print(a) # print the id
寫的時候,
a = ec[0].id # Create a variable called "a" that points to the value of the "id" attribute of the first element in the list
print(ec2.Instance(a)) # Create a new instance of ec2.Instance() with id equal to the value at a, then call the .__str__ method, and print
^ 這實際上是多余的。
調用print(ec[0])
應該產生ec2.Instance(id='i-0ae514dcs36chb154')
因此,它們不同的原因在於,首先您要打印ec2.Instance()
對象的屬性id
的值。 在第二個中,您將打印ec2.Instance()
對象本身,它將調用ec2.Instance()
對象上的私有方法__str__
以生成要打印的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.