0

I'm trying to create a package with a module with a class that receives arguments and then selects a method within the class based on one of the arguments

import calcs.myModule as module

arg1      = 'state'  # this argument is used to determine which calculation to run
arg2      = 5  # example

myObject = module.classCall(arg1, arg2) 

results = myObject.calculate()

print(results)

Names have been changed for readability The module looks like this

class classCall(object):

    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

    def state1(self):
        # calculations preformed here
        return results

    def calculate(self):
        if self.arg1 == "state1"
            results = state1(self)

But I'm receiving an error still

    results = state1(self)
NameError: name 'state1' is not defined

I thought this should work. Why isn't it and what should I do?

  • 2
    Reference it via `self`. `self.state1()` https://realpython.com/blog/python/instance-class-and-static-methods-demystified/ – Fallenreaper Oct 24 '17 at 01:45
  • As fallenreaper has linked: state1 is a method that is tied to an instance of your class `classCall`. To invoke said method use `self.state1()` In addition to the above, see: https://pythontips.com/2013/08/07/the-self-variable-in-python-explained/ – silent Oct 24 '17 at 01:49

0 Answers0