0

When trying to run the listPersons() command, every Person/Instance should call the sayHello() method. But as the names are str, it would raise an AttributeError (see below).

How do I format the names so I can use the methods on them?

class person:
    def __init__ (self, name):
        self.name = name
    def sayHello(self):
        print("Hello World, I'm", self.name)

def listPersons():
    print ("There are", len(names), "persons here, please everybody say hello to the world!")

    for name in names:
        print(name.sayHello())

names = ["Tobias", "Lukas", "Alex", "Hannah"]

for name in names:
    globals()[name]  = person(name)

AttributeError:

Traceback (most recent call last):
File "<pyshell#97>", line 1, in <module>
listPersons()
File "/Users/user/Desktop/test.py", line 12, in listPersons
print(name.sayHello())
AttributeError: 'str' object has no attribute 'sayHello'

Thank you so much for your help! :-)

1
  • I copy pasted this code and am not getting an error in both Python 2 and 3 Commented Sep 1, 2016 at 23:57

1 Answer 1

2

You're getting this error because names list is a list of strings, not the people objects you create. Since you are using globals(), each person is being assigned to a variable in the global scope. Rather than using globals(), I would suggest having a list of people.

Another problem you will run into is that you are trying to print the output of person.sayHello, but that does not return anything. You could just call the function instead.

Both of these changes together:

class person:
    def __init__ (self, name):
        self.name = name
    def sayHello(self):
        print("Hello World, I'm", self.name)

def listPersons():
    print ("There are", len(people), "persons here, please everybody say hello to the world!")

    for name in people:
        name.sayHello()

names = ["Tobias", "Lukas", "Alex", "Hannah"]
people = []

for name in names:
    people.append(person(name))
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your helpful answer and explanation! But is there a way to create the instances in a way I can still call Tobias.sayHello() ?
solved it by adding following line after people.append(person(name)): globals()[people[i].name] = people[i]! Is this a valid way to solve it?
I would advise against using globals because it is a round about way to create or access variables. If I see Tobias.sayHello(), I expect to see a line like Tobias = person("Tobias"). Using globals() in this way makes the code less readable could open you up to other bugs. For example, what if the names list has a values "person". One other alternative I would suggest is storing the people in a dictionary. Something like people = {}, then people[name] = person(name). Then you could do people["Tobias"].sayHello().

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.