To learn other people's works, you have a great advantage and can look at your own shortcomings.
Your own shortcomings are often due to lack of basic knowledge and poor basic skills.
Today, I want to add another lesson: asterisk expression (* expression ).
In the mine mining program, two asterisks are used. One is in Main. py, and the other is in game_scene.py.
First, let's look at the first situation:
@pyqtSlot()def on_action_Setup_triggered(self): result = setup.getSetup() if not result: return self.scene.setMap(*result) self.scene.start()
The result here is returned from setup. py, which is like this:
def getSetup(): if SetupDlg().exec_() != QDialog.Accepted: return return (conf.w, conf.h), conf.mines
That is, result = (Conf. W, Conf. h), Conf. Mines)
Then, parse the result with an asterisk. After calling the self. Scene. setmap (* result) function,
In game_scene.py:
def setMap(self, size, mines): self.setup = size, mines self.map_size = size self.mines = mines
It can be seen that * The result is parsed into two objects, one being the size of the tuples and the other being the integer mine.
If * result Parsing is not required, the header parameter of the setmap function is an object of result, which must be parsed again.
The following function new_func uses the asterisk expression in the second part, which is also common:
def inGame(func): """ check if this game is running """ def new_func(self, *args, **kw): if self.status != RUNNING: return return func(self, *args, **kw) return new_func
Note! First, the asterisk expression can only be used in the function header. Otherwise, it is a syntax error;
Second, the asterisk is important, not the parameter name;
Third, asterisks mean that the parameter belongs to the iterable type, and its length can be measured using the Len () function.
The following is a simple example:
>>> def func(*S,**K):print(S,'\n',K)...>>> func(1,[2,3],a=5,b='6',c='asd')(1, [2, 3]) {'a': 5, 'c': 'asd', 'b': '6'}>>>
Pyqt mining mine Game Study Notes (6)