-1

I am trying to append to a list by using a Class, however I get the following error: TypeError: __init__() takes 1 positional argument but 3 were given

This is the code:

lijst = []

# Class voor Student
class Student:
# Constructor
def __init__(self):
    self.__achternaam = ''
    self.__voornaam = ''

# Attribuut achternaam
def setAchternaam(self, achternaam):
    self.__achternaam = achternaam

def getAchternaam(self):
    return self.__achternaam
achternaam = property(getAchternaam, setAchternaam)

# Attribuut voornaam
def setVoornaam(self, voornaam):
    self.__voornaam = voornaam

def getVoornaam(self):
    return self.__voornaam
voornaam = property(getVoornaam, setVoornaam)

lijst.append(Student('Quinten' , 'Vollmer'))
lijst.append(Student('Jochem' , 'Legue'))
lijst.append(Student('Johan' , 'Jansen'))

I have tried the following:

class Student:
# Constructor
def __init__(self, achternaam, voornaam):
    self.__achternaam = achternaam
    self.__voornaam = voornaam

However the output becomes:

[<__main__.Student object at 0x0000017614279FD0>, <__main__.Student object at 0x00000176142798E0>, <__main__.Student object at 0x00000176142797F0>]

Don't know how to fix it.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
MisterQ
  • 1
  • 1
  • 1
    You solved the first problem, but the second behaviour is expected. If you want the output to look different, you'll have to write a function/method for it, which could be `__repr__` or `__str__` if you want that function to be called implicitly when printing. – trincot Oct 23 '21 at 19:58
  • What is the output you are expecting? – chickity china chinese chicken Oct 23 '21 at 20:34
  • I want to add the first names (voornaam) and last names(achternaam) to the list. So the list should contain: Quinten Vollmer, Jochem Legue, Johan Jansen – MisterQ Oct 23 '21 at 20:39

1 Answers1

0

I'm pretty sure this

    class Student:
    # Constructor
    def __init__(self, achternaam, voornaam):
       self.__achternaam = achternaam
       self.__voornaam = voornaam

is correct. You're probably just getting a weird looking output because your data type can't be printed as easily as a string or something else that Python knows how to print, so it's just telling you where the object is stored in memory.

V1V3
  • 37
  • 5
  • Okay thanks, how do I change it to get the correct output? – MisterQ Oct 23 '21 at 20:03
  • You would need to write a __str__ method, like this: def __str__(self) -line break- return ("the value of voornam is: " + self.voornam) – V1V3 Oct 23 '21 at 20:10