Problem with classes

python

#1

After completing the module on classes, I decided to try and implement them in my game that I'm creating, but I'm stuck trying to figure out how to pass an object between functions that are in separate files (modules)?

I instantiate my object newPlayer at the end of main.py and when I select D for the debug menu, then select P for the find_potion() method it crashes with the following error

  File "main.py", line 64, in <module>
    menu()
  File "main.py", line 49, in menu
    debug.menu(newPlayer)
  File "/home/bran/Dungeon-Quest/debug.py", line 37, in menu
    player.find_potions()
AttributeError: 'module' object has no attribute 'find_potions'

I know that to call a method of a class from within the module (?) it exists in is as simple as object.method() but I'm confused on how to do it from outside that module. Since my object newPlayer is instantiated within main.py and debug.py is imported and called from within main.py do I need to explicitly pass the object to the debug function as an argument?

Also how do I access the methods of the object from within the debug function? This is what really confused me since I'm not sure whether I should use the object name (newPlayer) or the class name (actions) or the module name along with the class name (player.actions).

Here's my GitHub repo: https://github.com/brando56894/Dungeon-Quest


#2

Hello, I don't know about python, started to learn a few weeks.
I found this: you need to check
1) You're importing the module, not the class. "from player import player".
2) You're importing one module that depends on other, so need to be imported in order.
3) Mutual imports (bad structure), if one import depends on other and viceversa at the same time.


#3

It seems that my issue was with how I had my classes separated in player.py

player.create initialized the object, but had no methods. I was trying to call on the actions class which had inherited the member variables from the create class. Once I removed the class definition for actions everything worked as expected.

To answer my own question: yes you have to pass the object from main to debug and then within the debug function you call upon the methods of the object.