Use return
instead of print
in your total()
function:
class something(object):
def __init__(self, name=None, someobjects=[]):
self.name = name
self.someobjects = someobjects
def total(self):
return len(self.someobjects)
listofpeoplewhoarethisclass = [something("john", [1,2,3])]
for person in listofpeoplewhoarethisclass:
print person.name, person.total(), len(person.someobjects)
returns: john 3 3
Hint: I don't know if you just named your class "something" or the list "listofpeoplewhoarethisclass" just for the question, but please give your classes, variables and lists useful and intuitive names (e.g. instead of "total" name the function "length" or "size", because total sound like a sum to me).