简体   繁体   中英

Call Function of inside function in Python

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?

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?

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.

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