4

I have some Python code in below written in Python 2.7 and I have problem with calling a function form inside another function.

class CSP:

def __init__(self, matrix):
    self.X = []
    self.D = []
    self.C = []
    self.matrix = util.copyMatrix(matrix)
    self.counter = 0
    # Matrix to Vector
    vector = [item for line in self.matrix for item in line]
    chars = map(str, vector)
    result = ['*' if item == '0' else item for item in chars]

def solve(self):
    """ Returns the result matrix.
    The sudoku matrix is self.matrix.
    Use util.printMatrix in purpose of debugging if needed. """

    "*** YOUR CODE HERE ***"
    def init(self,result):
        for i in range(9):
            for j in range(1,10):
                var = var_char[i]+str(j)
                self.X.append(var)
                domain = set([1,2,3,4,5,6,7,8,9])
                self.D.append(domain)
        gamelist = result
        for i in range(len(gamelist)):
            if(re.match("\d+",gamelist[i])):
                self.D[i] = set([int(gamelist[i])])
        self.set_constraints()

      #########################################################################
    def set_constraints(self):
        for x in self.X:
            for y in self.X:
                if((x[0] == y[0] and x[1] != y[1]) or (x[1] == y[1] and x[0] != y[0])):
                    flag = True
                    for c in self.C:
                        if(x in c and y in c):
                            flag = False
                    if(flag):
                        self.C.append(set([x,y]))

        for a in [0,3,6]:
            for b in [0,3,6]:
                self.set_cube_constraints(a,b)

How to call init() function in solve() and also call self.set_constraint() inside init() function?

5
  • It's a bad idea to call __init__ inside other method. I don't think you're able to call self.set_constraint() inside __init__, if you have a need for this, you should define set_constraint as a regular method. Commented May 18, 2015 at 11:29
  • 1
    @laike9m: the method the OP is talking about is init(), not __init__() Commented May 18, 2015 at 11:39
  • 1
    @SsRr: please fix your indentation. Commented May 18, 2015 at 11:40
  • @brunodesthuilliers I see. Then there's not much to say. Commented May 18, 2015 at 11:42
  • 1
    You can't define class methods inside another one. To call init() from solve() — which is one — you'll need to use init(self, result) rather than self.init(result) as you normally would if it was a method. Likewise, you'll need to use set_constraints(self) instead of self.set_constraints(). Commented May 18, 2015 at 11:47

2 Answers 2

2

Within function solve(), init() is a function, not a method. Therefore it can only be called in the same manner that any other unbound function can be called: by passing the correct number of arguments to it. This would work:

init(self, results)

Note that you need to explicitly pass a reference to the object in self because init() is not a method. Within solve() self refers to the CSP instance, so this should work.

However, set_constraints() is also a normal function, so you can not call it from init() with self.set_constraints(), but set_constraints(self) should work. Note that you need to declare function set_constraints() before init() otherwise you will get a "referenced before assignment" error.

Having said all that, this is just awful. Why not make init() and set_constraints() proper methods of the class?

Sign up to request clarification or add additional context in comments.

Comments

0

set_constraints is not a part of the class and therefore cannot be called with self. If you put it one level up (remove one indentation level of it) then your code should work better.

I can see that this is some kind of coding exercise and you are told to write code in one particular place. I think you may be overcomplicating the answer because what you are coding here looks very messy by design and you should probably split out your functionality a lot more if this should be considerered clean code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.