Nov-30-2017, 09:54 AM
# Why does this function behave differently in a class vs. outside a class? class Test(): def add_person(element, lineage=[]): lineage.append(element) # AttributeError: 'str' object has no attribute 'append' return lineage z = Test() g = z.add_person("Test1") h = z.add_person("Test2") t = z.add_person("Test3") print(g, h, t) # >>Output: # AttributeError: 'str' object has no attribute 'append' def add_person3(element, lineage=[]): lineage.append(element) return lineage d = add_person3("1") e = add_person3("2") f = add_person3("3") print(d, e, f) # >>Output: # # >>Output: # ['1', '2', '3'] ['1', '2', '3'] ['1', '2', '3']