Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix TypeError: 'str' object is not callable?

pic

1

Try to simplfy the my question here, so used a,b, & c instead.

Even though I copied the tutorial's answer and run it through Colaboratoy, it still show TypeError:'str' object is not callable.

Why is that?

Thank you.

like image 293
Joan Avatar asked Oct 27 '25 02:10

Joan


2 Answers

you might somewhere have declared the print as a variable. eg: print = "sample". So restart the session or execute del print command and try executing your code.

    del print
    a=1
    b=2
    c=a+b
   
    print(c)

Try executing the above code, also follow the below rules while naming a variable in python.

  • The identifier must start with a letter or an underscore (_).
  • The identifier can contain letters, digits, and underscores.
  • The identifier cannot be a reserved word.

Here are some examples of valid and invalid identifier names in Python:

# Valid identifier names
_private
_private123
myVariable
myVariable123
MY_CONSTANT

# Invalid identifier names
123invalid  # cannot start with a digit
for  # for is a reserved word

like image 166
R Ashwin Avatar answered Oct 29 '25 16:10

R Ashwin


Answering the question title:

How to fix TypeError: 'str' object is not callable?

As a starter in python I feel the need to answer this because nor python nor editors (at least vscode with python plugin) seem to give a hint that you may have accidentally shadowed a methodName with a fieldName.

Consider the following:

# value field shadows value() method
class ValueWrapper:
    def __init__(self):
        self.value = 'someValue'

    def value(self):
        return self.value

print("value:", ValueWrapper().value())

output: TypeError: 'str' object is not callable

# value field does not shadow getValue() method
class ValueWrapper:
    def __init__(self):
        self.value = 'someValue'

    def getValue(self):
        return self.value

print("value:", ValueWrapper().getValue())


Output: value: someValue

like image 42
Marinos An Avatar answered Oct 29 '25 18:10

Marinos An



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!