0

The issue was a typo, thanks everyone who tried to help

I'm trying to run a unit test in travis-ci. At one point I have a class which simplified looks like this:

class X:

    def __init__(self, var1):
        self.var1 = var1

    def Y(self):
        return True

    def Z(self):
        return False

    def call_function(self, function):
        output = function(self.Y, self.Z, self.var1)

I then call X.call_function, which has given me no problems in the past when running the program. However, when running this in travis-ci I'm told:

'X' object has no attribute 'Y'

Strangely enough, it does not appear to have an issue with self.Z.

Does anybody know what causes this and how to fix it?

Update: For clarity, the function parameter would be something like:

def function(func1, func2, var1):
    if type(var1) == int:
        func1()
    else:
        func2()

And then the main file would be something like:

x = X(3)
x.call_function(function)
banzaiman
  • 2,631
  • 18
  • 27
Nathan
  • 3,558
  • 1
  • 18
  • 38

2 Answers2

1

Your argument of self.Y returns the function itself, not the value it returns. If instead your self.Y was set to True, the call_function function would work. To run the function, you can put brackets after the function name.

Edit: Looks like you've forgotten about adding self. No worries, here's the working code. (The self argument is called automatically when you make an instance of a class. For more info on self, check this out)

Edit 2: I've changed it so it returns the function. The self parameter is all that's needed.

Here's your fixed code:

class X:

    def __init__(self, var1):
        self.var1 = var1

    def Y(self):
        return True

    def call_function(self, function):
        output = function(self.Y, self.var1)
GeeTransit
  • 1,458
  • 9
  • 22
1

you have to add the self with the function in class if it is not @static,class or abstract method

class X:

    def __init__(self,var1):
        self.var1 = var1

    def Y(self):
        return True

    def call_function(self, function):
        output = function(self.Y(), self.var1)
sahasrara62
  • 10,069
  • 3
  • 29
  • 44