Python Forum
error in class: TypeError: 'str' object is not callable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error in class: TypeError: 'str' object is not callable
#1
in below code:
class Person:
     def __init__(self,username):
         self._username= username

     #getter
     @property
     def username(self):
         return  self._username

     #setter
     @username.setter
     def username(self,name):
         if not isinstance(name, str):
             raise TypeError('expected a string object.')
         self._username=name.lower()

p=Person('ali')
print(f"p.username is :{p.username}")
p.username('mohammad')
print(f" after p.username('mohammad'), p.username results: {p.username}")
after run, the below error appeared:

Error:
Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> p.username("mohammad") TypeError: 'str' object is not callable
what is the problem?
thanks
Reply
#2
(Dec-30-2023, 07:51 AM)akbarza Wrote: what is the problem?
The problem is that p.username is the string 'ali'. So if you write p.username('mohammad'), it is as if you write 'ali'('mohammad') which is a type error. The correct syntax is p.username = 'mohammad'.
akbarza likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
The problem is Person.username is a property. Properties are referenced like attributes, even though they look like a method in your code. The correct way to set a property is use assignment.
p.username = 'mohammad'
I would call this a bug:
     def __init__(self,username):
         self._username= username
According to the username setter, usernames have to be lower case, but your code allows initializing a Person with a username that is not lower case.
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing out incidence values for Class Object SquderDragon 3 304 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 413 Mar-07-2024, 03:40 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 762 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 1,021 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,390 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 861 Jul-23-2023, 02:25 PM
Last Post: Fare
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,275 Jun-09-2023, 06:56 PM
Last Post: kpatil
  TypeError: 'float' object is not callable #1 isdito2001 1 1,091 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 4,190 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  'SSHClient' object is not callable 3lnyn0 1 1,178 Dec-15-2022, 03:40 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020