0

Hi I am trying to understand creating custom classes, instances and methods in python but things aren't working as I expect.

I created a test class as follows:

class otest:

def __init__ (self, name, l_values=[]):
    self.name = name
    self.values = l_values

def add_val (self,value):
    self.updated = self.values.append(value)

I load it up with some test data and run the add_val method:

a=otest('can',[1,2,3])
a.add_val('a')

When I call:

a.values

I get the expected list of [1,2,3,'a'] but when I run:

a.updated

It returns an empty list despite setting the new class attribute (updated) equal to the self.values.append(value). Given a.values shows an updated list, the append part is working but it seems I can't set the new attribute to this list? Can someone explain the logic and why?

Thanks for the help!

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
xovut
  • 3
  • 2

0 Answers0