0

I was just practicing class and object The code is enter image description here

I already know the second function def name is overlapped since name is already set as self.name. but I am not sure how it makes error.

Can anyone tell how to fix this problem?

2 Answers 2

0

The error is pretty descriptive; strings aren't callable, functions are. I believe Python will look for variables within the class before it checks if there are functions with a given name. Thus, the string name is found but because it has () added to it, python then tries to call it like a function and fails.

Simply rename the function or the variable. You could use self._name as the variable name if you want to indicate that it's private and shouldn't be changed - at least not from outside the class. You'll also have to write print "The University name is %s" % (self._name) (and self.rank).

Sign up to request clarification or add additional context in comments.

Comments

0

You already have a variable called name in your class University, so the program is trying to call "University of America"(), which triggers an error. Thus, change the function name to something like print_name:

class University():
        def __init__(self, name, rank):
                self.name = name
                self.rank = rank

        def print_name(self):
                print "The University name is %s" % (self.name)
                print "This University is ranked at %s" % (self.rank)

>>> users = University("University of America", "#1")
>>> users.name #The string of the name
'University of America'
>>> users.print_name()
The University name is University of America
This University is ranked at #1
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.