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.