简体   繁体   中英

Python : how to call a function in another Class

I'm stuck with some Python script i found at https://python4kids.brendanscott.com/2014/12/02/hooking-up-the-sunfish-chess-engine-advanced/ : i followed the instructions of Brendan Scott and build the little Python script as he described, to get a TKinter GUI for sunfish.py, a nifty little chess application. But the code contains some bugs, although his article and explanations are very clear and well set up.

First, this gave a "KeyError" error :

def location_to_algebraic(board_location):
    return "%s%s"%(ALGEBRAIC_DICT[7-board_location.j],8-board_location.i)

which i simply solved by:

def location_to_algebraic(board_location):
    return "%s%s"%(ALGEBRAIC_DICT[math.ceil(7-board_location.j)],math.ceil(8-board_location.i))

Explanation : the point on the screen where the user clicked, somewhere on a chess board square, seems to give x,y float numbers while integers are expected, because they are the index of a dictionary. Just by rounding, using math.ceil(), we get the proper integer and it works as intended. Strange, it seems the author did not test the final script.

But another (simple?) bug in this script i can not solve :

move, score = sunfish.search(pos)

gives this error : AttributeError: module 'sunfish' has no attribute 'search'

It seems the search() function is not called properly, while it DOES exist in module 'sunfish' : in its Class 'Searcher'. So i tried to fix it by:

move, score = sunfish.Searcher.search(pos)

but then i get another error:

TypeError: search() missing 2 required positional arguments: 'pos' and 'secs'

The search() function is now called, but with to few arguments !? When i try to fix this by:

move, score = sunfish.Searcher.search(pos, secs=2)

i get another error:

TypeError: search() missing 1 required positional argument: 'pos'

I stuck now .. Here is the concerning search function, inside the sunfish.Searcher class, which is very simple:

def search(self, pos, secs):
    start = time.time()
    for _ in self._search(pos):
        if time.time() - start > secs:
            break
    return self.tp_move.get(pos), self.tp_score.get((pos, self.depth, True)).lower

How can i call search() properly ?

The init of the Searcher class is like this :

class Searcher:
    def __init__(self):
        self.tp_score = LRUCache(TABLE_SIZE)
        self.tp_move = LRUCache(TABLE_SIZE)
        self.nodes = 0

The sunfish.Searcher.search() function takes 3 arguments, the first variable is self which is reference to the current instance of the class. Thus, when you call the search function without creating a sunfish.Searcher object, the self variable is not provided automatically and self gets the value of pos and secs gets the value of 2.

To solve this, you need to create a sunfish.Searcher object first and then call the search function through that object.

Example:-

Obj = sunfish.Searcher()
Obj.search(pos, secs)

Here is an article which clearly explains the concepts of classes and objects in python:- https://www.programiz.com/python-programming/class

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