简体   繁体   中英

How to use the sum function in for loop?

I am using the sum function in my loop.

After calling this function I am getting error:

"AttributeError: 'int' object has no attribute 'num_children'"

class _InnerNode(_Node):
    def __init__(self, ctr_idx, level, radius, children):
        self.ctr_idx = ctr_idx
        self.level = level
        self.radius = radius
        self.children = children
        self.num_children = sum(c.num_children for c in children)

c is an integer not an instance of the class _InnerNode so the compiler can't find the attribute that's why it is throwing you an error (as said in the comments).

Since children is a list the correct code for the last line is : self.num_children = sum(c for c in children)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM