Creating an instance of a class on Python from imported module

Viewed 15

Given the files

#module.py

def greet(name):
    print('greetings ' + name)

#greet('Ana') would return "greetings Ana"

class Greetings():
    def good_morning(self,name):
        print('good morning ' + name)

#x = Greetings()
#x.good_morning('Bob') would return "good morning Bob"

and

#program.py

import module

module.greet('Ana')

x = Greetings()
x.good_morning('Bob')

When I run module.py (without the #'s) it works as expected. And when I run program.py it actually returns "greetings Ana" (so far so good). Now, I know that you can't (shouldn't?) call a class directly, that's not how OOP works on Python. You should create an instance of that class and then call the methods in it.

So what am I missing? Because when I run the program with the last 2 lines it returns:

Traceback (most recent call last):
  File "C:\Users\..\program.py", line 7, in <module>
    x = Greetings()
NameError: name 'Greetings' is not defined
[Finished in 110ms]
0 Answers
Related