-1

I was trying to write a code and check it with pytest. I thought I did everything ok, but I got a problem. After writing this, I want to check it with method of class Person. When I tried to use method id the function ends and I got the output:

 TypeError: 'str' object is not callable

My code:

class Person:
    def __init__(self, id, name, sex, birth_date):
        self.name = name
        self.id = id
        self.sex = sex
        self.birth_date = birth_date

    def __str__(self):
        return f'{self.name} born {self.birth_date} with {self.sex} has {self.id}'

    def name(self):
        return self.name

    def sex(self):
        return self.sex

    def id(self):
        return self.id

    def date_of_birth(self):
        return self.birth_date

def read_from_file(file_handle):
        people = []
        reader = csv.DictReader(file_handle)
        for row in reader:
            id = row['id']
            name = row['name']
            sex = row['sex']
            birth_date = row['birth_date']
            person = Person(id, name, sex, birth_date)
            print(person.id())[Error in this line]
            people.append(person)
        return people

def test_files():
    with open('people.txt', 'r') as people_txt:
        people = read_from_file(people_txt)

Example of line of people.txt:

id,name,sex,birth_date

1,Doralyn Dovermann,Female,27/10/2005
2,Rickert Petschel,Male,10/7/2018
3,Whitney Girardoni,Female,7/3/1932
New_stud
  • 13
  • 3
  • 1
    Which line does the error message point to? – mkrieger1 Dec 13 '21 at 00:27
  • 1
    Please always include the complete Traceback. Format it as code. – wwii Dec 13 '21 at 00:30
  • 1
    Line number is missing, also just a tip: you have id as a function and then you are defining the attribute variable within your constructor as `self.id`, might be better to rename either one to keep the naming convention clean. – de_classified Dec 13 '21 at 00:30
  • It points the print(person.id()). I don't know why it works like that, but I think that person is not an object. – New_stud Dec 13 '21 at 00:32
  • 1
    It works like that because you are instantiating `Person()` class and carries all the method with it to the defined `person`. Possibly look into Inheritance and OOP topics. – de_classified Dec 13 '21 at 00:34
  • So this is why I got output : TypeError: 'str' object is not callable. I know what you mean, but I don't think it causes a problem. – New_stud Dec 13 '21 at 00:37
  • Again, like everyone else mentioned, include the full traceback with the line number. – de_classified Dec 13 '21 at 00:38
  • Comment out these methods `def name(self): ..., def sex(self): ..., def id(self): ..., def date_of_birth(self): ...` then try it. – wwii Dec 13 '21 at 00:41
  • May be of interest: [Using @property versus getters and setters](https://stackoverflow.com/questions/6618002/using-property-versus-getters-and-setters) – wwii Dec 13 '21 at 00:51
  • 3
    You're using the same names for your methods that you use for the attributes they're supposed to return. That can't work. An attribute lookup like `person.id` can only find either the attribute *or* the method, not both (instance attributes take precedence). It's not obvious why you're written the methods at all (getter methods are not generally used in Python). – Blckknght Dec 13 '21 at 00:56

1 Answers1

2

That error occurs when you try to call a string as if it were a function. In your code you call person.id(), but person.id is not a function. If you want to print the contents of the string attribute, just do print(person.id).

jordanm
  • 33,009
  • 7
  • 61
  • 76
  • Sorry for disturbing but I want to be sure about what I do. Why method person.id() doesn't works. I haven't found errors like that when I tried to call the method of class. – New_stud Dec 13 '21 at 00:46
  • @New_stud read his answer again; expecially the "but person.id is not a function"-part – seldomspeechless Dec 13 '21 at 01:11
  • So as I understand, everything is because of names of functions? I' am so sorry, I didn't expect that the problem is not very deep. I was focused on looking for small mistake or smth like that. – New_stud Dec 13 '21 at 01:25
  • @New_stud It is a small mistake, a `()` where there shouldn't have been. Adding `()` to the end of a name is telling python to call/execute that name. Basic objects like strings can not be executed. – jordanm Dec 13 '21 at 02:08