Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python OOP datatype

Why does it say ‘Die eingegebenen Daten haben den falschen Datentyp!’ when the datatypes are actually right? Doesn’t even work with just matrNr… Although I checked my input of matrNr to be an int?!

class Student:
    def __init__(self):
        self.matrNr = -1
        self.vorname = ''
        self.nachname = ''
        self.gebDatum = []
        self.email = ''
        self.telNr = ''

    def DatenUebergeben(self, matrNr, vorname, nachname, gebDatum, telNr):  
        if matrNr == int and vorname == str and nachname == str and gebDatum == list and telNr == str:
            print('richtige Datentypen!')
        else:
            print('Die eingegebenen Daten haben den falschen Datentyp!')

student1 = Student()
student1.DatenUebergeben(12345,'linda','f',[2,2,1995],'12345')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Background

By checking (for example) matrNr == int you actually compare the variable matNr, which is an int (an instance of <class 'int'>) to the class int (<class 'int'>).

Quick fix

You can use the type()-function to get the type of a variable, resulting in type(matrNr) == int. This does exactly what you are trying to achieve.

Better solution

In Python you can define the types of variables a function accepts by adding : <type> after the argument. The better solution would thus be:

def DatenUebergeben(self, matrNr: int, vorname: str, nachname: str, gebDatum: list, telNr: str):
    # do something
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading