-1

I'm trying to create a dict within a class with keys as prompt-commands and class methods as values, but I'm getting below when trying to run the module

cmd = {'-v': self.view_leagues(), NameError: name 'self' is not defined

I do not understand why I'm not able to do this?

Example code below:

class CLIStats:

    cmd = {'-v': self.view_leagues(),
           'exit': 'sys.exit()',
           'Back To Home': '-b',
           'View Stats Type': '-s',
           'Help' : '-h' }

    def __init__(self):
        self.leagues = {'EN_PR': ['2019/2020', '2018/2019']}

    def view_leagues(self):
        for league in self.leagues.keys():
            print("{: <10}".format(league), end="")
        print('\n')


def main():
    interface = CLIStats()
    print(interface.cmd.keys())


if __name__ == '__main__':
    main()
MisterButter
  • 749
  • 1
  • 10
  • 27

1 Answers1

1

You just need declare the variables inside __init__(). Try this:

class CLIStats:
    def __init__(self):
        self.leagues = {'EN_PR': ['2019/2020', '2018/2019']}
        self.cmd = {'-v': self.view_leagues(),
               'exit': 'sys.exit()',
               'Back To Home': '-b',
               'View Stats Type': '-s',
               'Help' : '-h' }

    def view_leagues(self):
        for league in self.leagues.keys():
            print("{: <10}".format(league), end="")
        print('\n')


def main():
    interface = CLIStats()
    print(interface.cmd.keys())


if __name__ == '__main__':
    main()

If you want to create cmd ad standalone variable use the following code:

class CLIStats:
    def __init__(self):
        self.leagues = {'EN_PR': ['2019/2020', '2018/2019']}

    def view_leagues(self):
        for league in self.leagues.keys():
            print("{: <10}".format(league), end="")
        print('\n')


def main():
    interface = CLIStats()
    cmd = {'-v': interface.view_leagues(),
           'exit': 'sys.exit()',
           'Back To Home': '-b',
           'View Stats Type': '-s',
           'Help' : '-h' }
    print(cmd.keys())


if __name__ == '__main__':
    main()
Prudhvi
  • 1,095
  • 1
  • 7
  • 18
  • I see, thank you for the input! If I may ask, why must `cmd` be declared in the `__init__` method and cannot not be a stand alone variable? – MisterButter Apr 23 '20 at 09:41
  • Because you are using a function which is internal to class and you need to add `self` to `cmd` because you are accessing in `print(interface.cmd.keys())` using the instance of the class.. – Prudhvi Apr 23 '20 at 09:42
  • I understand! Because the problem is the when `cmd` is declared in `__init__()` the `self.view_leagues()` is called when executing the module, but I want the method to be executed on command. – MisterButter Apr 23 '20 at 09:45
  • Thank you, it makes sense on why I can't do what I was trying to do! – MisterButter Apr 23 '20 at 09:50
  • I've been testing above and it doesn't work as intended, that functions inside `cmd` are executed when `cmd` is declared. But values inside `cmd` should only be executed if the user prompts the key to that specific value – MisterButter Apr 23 '20 at 10:51
  • @MisterButter you can check this https://stackoverflow.com/questions/46106779/dictionary-value-as-function-to-be-called-when-key-is-accessed-without-using – Prudhvi Apr 23 '20 at 11:06