简体   繁体   中英

Python NameError: name '...' is not defined in a class

I'm trying to make a cool little turn based fighting game and one part of my code doesn't like me. I have a menu system that can't determine a few things. (Please keep in mind, I just started classes very recently.)

Making a class entity (for players and enemies).

class Entity:
  def __init__(self):
    self.name = ''
    self.health = 0
    self.moveset = []

Making a player class (so I can add more characters) with a menu inside.

 class Player(Entity):
      options = [menu(),attack(),items(),stats(),flee()]
      def menu(self):
        menuchoice = input('Menu\n1. Attack\n2. Items\n3. Stats\n4. Flee\n')
        if menuchoice not in ['1','2','3','4']:
          clear()
          options[0]
        options[int(menuchoice)]
        options[0]
      def attack(self):
        print('attack')
      def items(self):
        print('items')
      def stats(self):
        print('stats')
      def flee(self):
        print('flee')

Trying to run the menu

player = Player()
player.menu()

Error

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    class Player(Entity):
  File "main.py", line 17, in Player
    options = [menu(),attack(),items(),stats(),flee()]
NameError: name 'menu' is not defined

Can someone tell me how to define the menu in this code? I'm using Python 3.6.1.

Edit: Thanks! It works now. I had to add () and move options = [...] to the end!

The variable options is likely intended to maintain a reference to the available options. What it's doing instead is calling the method itself before even the method is defined:

Here's a minimal version that demonstrates the problem (it's on the python interpreter itself:

>>> class Player:
...     options = [menu()]
...     def menu(self):
...         print('menu')
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in Player
NameError: name 'menu' is not defined
>>>

Here's another version without actually calling the method but just adding the reference. It'll hit the same error:

>>> class Player:
...     options = [menu]
...     def menu(self):
...         print('menu')
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in Player
NameError: name 'menu' is not defined
>>>

Here's probably what you want:

>>> class Player:
...     options = ['menu']
...     def menu(self):
...         print('in menu')
...
>>> player = Player()
>>> player
<__main__.Player instance at 0x10a2f6ab8>
>>> player.options
['menu']
>>> player.options[0]
'menu'
>>> type(player.options[0])
<type 'str'>
>>> func = getattr(player, player.options[0])
>>> type(func)
<type 'instancemethod'>
>>> func
<bound method Player.menu of <__main__.Player instance at 0x10a2f6ab8>>
>>> func()
in menu
>>>

Here's the proof if you use it after defining, it won't hit the error - but this isn't the standard/typical usage:

>>> class Player:
...     def menu(self):
...         print('in menu')
...     options = [menu]
...
>>> player = Player()
>>> player.options
[<function menu at 0x10a2f16e0>]
>>> player.options[0]
<function menu at 0x10a2f16e0>
>>> player.options[0]()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: menu() takes exactly 1 argument (0 given)
>>> player.options[0](player)
in menu
>>> Player.options[0](player)
in menu
>>> Player.options[0](player)   # calling with class reference
in menu
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM