python 返回值是个对象,Python返回值仅显示对象中的一个值,而不是所有值

I have a Python file with two class A and B, and I am inheriting temp from class A to B. In my class A function temp, I am getting the id value from an XML file which has values like this,

[1,2,3,4,5]

My Class A with temp function

class A:

def temp(self):

id = []

for child in root:

temp_id=child.get('id')

id.append(temp_id)

return id

and I am getting the function value in class B,

class B (A):

fun=A

value = fun.temp()

for x in value:

return x

While printing the return value instead of printing all the values, I am getting only the first value

[1,1,1,1,1]

Can some one please let me know on how to solve this, thanks.

解决方案

Standard functions can only return a single value. After that, execution continues from where that function was called. So, you're trying to loop through all the values, but return upon seeing the first value, then the function exits and the loop is broken.

In your case, you could simply return fun.temp(). Or, if you want to process each value, then return the new list, you could run something like:

new_list = []

value = fun.temp()

for x in value:

# do something with x here

new_list.append(x)

return new_list

Also, it appears you may be a bit confused about how inheritance works. DigitalOcean has a great article on it, if you want to take a look.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值