简体   繁体   English

为什么函数的 (self, index) 的索引没有在函数内部被调用并且函数仍然有效?

[英]Why function's (self, index)'s index doesn't get called inside the function and the function still works?

I have this snippet of working and tested Python code:我有这个工作和测试的 Python 代码片段:

def renameFiles(self, index):
    filename = self._filenames[self._current_index]
    newFilename = self.createNewFilename()
    self.currentFileName.setText(newFilename)
    dirname = os.path.dirname(filename)
    fullPath = os.path.join(dirname, newFilename)
    os.replace(filename,fullPath)
    self._filenames[self._current_index] = fullPath
    self.current_index = self._current_index

Why is it that the index in the def renameFiles(self, index): isn't called in anywhere inside the function and yet the function still works?为什么,在高清renameFiles(个体经营,指数)指数未在功能,但功能仍然有效内的任何地方叫什么名字? (It may not be the most optimized and beautiful code, but still works) (它可能不是最优化和最漂亮的代码,但仍然有效)

I've searched for manuals and guides for hours now and still can't wrap my head around this.我已经搜索了几个小时的手册和指南,但仍然无法理解这一点。 And I can't progress in my project until I understand how this works.在我了解它的工作原理之前,我无法在我的项目中取得进展。

I'll gladly post more of the code if needed.如果需要,我很乐意发布更多代码。

In python, a user-defined function can have as many "non-callable" arguments as you want.在 python 中,用户定义的函数可以有任意多的“不可调用”参数。 For example, the following is a valid function:例如,以下是一个有效的函数:

def f(a, b, c, d, e):
    print("Hello World")

However, unless you provide default values for variables a through e, calling the function without specifying them will raise an exception但是,除非您为变量 a 到 e 提供默认值,否则在不指定它们的情况下调用函数将引发异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM