Python 函数中的参数似乎没有影响?

Python 函数中的参数似乎没有影响?,python,list,function,Python,List,Function,我使用了一个函数,它允许我将文件的数据写入列表,但是我肯定遗漏了什么,因为我的函数中定义列表的参数似乎不起作用,你知道问题出在哪里吗? 这是我的职责: filePath = path + "\ONLYIVENOTFIXED.txt" listObj = [] i = listObj def writeFileOnAList(pathofThefile, namelist): fichierIve = open(pathofThefile, "r") namelist = f

我使用了一个函数,它允许我将文件的数据写入列表,但是我肯定遗漏了什么,因为我的函数中定义列表的参数似乎不起作用,你知道问题出在哪里吗? 这是我的职责:

filePath = path + "\ONLYIVENOTFIXED.txt"
listObj = []
i = listObj


def writeFileOnAList(pathofThefile, namelist):

    fichierIve = open(pathofThefile, "r")
    namelist = fichierIve.readlines()
    namelist = [x.strip() for x in namelist]
    i = namelist
    i = 0


writeFileOnAList(filePath, listObj)
print(listObj)
它告诉我函数中的“名称列表”设置未被使用,当我调用函数并尝试打印列表时,它会打印一个空列表


您有什么解决方案吗?

您的脚本中确实有很多错误:

filePath = path + "\ONLYIVENOTFIXED.txt"
listObj = []

# You are declaring the variable "i" here but you are never using it
i = listObj


def writeFileOnAList(pathofThefile, namelist):

    fichierIve = open(pathofThefile, "r")

    # You are parsing your listObj as parameter (namelist) but you never use it
    # instead you are just overwriting it
    namelist = fichierIve.readlines()
    namelist = [x.strip() for x in namelist]

    # Here you are overwriting your i variable 2 times in a row and never work with it 
    # after that
    i = namelist
    i = 0


writeFileOnAList(filePath, listObj)
print(listObj)
我不太确定你想做什么,但这是我的返工版本:

filePath = path + "\ONLYIVENOTFIXED.txt"


def writeFileOnAList(pathofThefile):
    fichierIve = open(pathofThefile, "r")
    namelist = fichierIve.readlines()
    namelist = [x.strip() for x in namelist]

    return namelist


listObj = writeFileOnAList(filePath)
print(listObj)

您的脚本中确实有很多错误:

filePath = path + "\ONLYIVENOTFIXED.txt"
listObj = []

# You are declaring the variable "i" here but you are never using it
i = listObj


def writeFileOnAList(pathofThefile, namelist):

    fichierIve = open(pathofThefile, "r")

    # You are parsing your listObj as parameter (namelist) but you never use it
    # instead you are just overwriting it
    namelist = fichierIve.readlines()
    namelist = [x.strip() for x in namelist]

    # Here you are overwriting your i variable 2 times in a row and never work with it 
    # after that
    i = namelist
    i = 0


writeFileOnAList(filePath, listObj)
print(listObj)
我不太确定你想做什么,但这是我的返工版本:

filePath = path + "\ONLYIVENOTFIXED.txt"


def writeFileOnAList(pathofThefile):
    fichierIve = open(pathofThefile, "r")
    namelist = fichierIve.readlines()
    namelist = [x.strip() for x in namelist]

    return namelist


listObj = writeFileOnAList(filePath)
print(listObj)

关键问题是Python是一种按对象引用传递的语言,而不是按变量引用传递:即,对象引用是按值传递的。因此,在函数中指定给namelist只会更改该变量的值:它对仍然引用原始列表的listobj没有任何影响

修复此问题的最具python风格的方法是函数返回名称列表:

filePath = path + "\ONLYIVENOTFIXED.txt"

def writeFileOnAList(pathofThefile):
    with open(pathofThefile, "r") as ficiherIve:
        namelist = fichierIve.readlines()
        namelist = [x.strip() for x in namelist]
    return namelist

listObj = writeFileOnAList(filePath)

关键问题是Python是一种按对象引用传递的语言,而不是按变量引用传递:即,对象引用是按值传递的。因此,在函数中指定给namelist只会更改该变量的值:它对仍然引用原始列表的listobj没有任何影响

修复此问题的最具python风格的方法是函数返回名称列表:

filePath = path + "\ONLYIVENOTFIXED.txt"

def writeFileOnAList(pathofThefile):
    with open(pathofThefile, "r") as ficiherIve:
        namelist = fichierIve.readlines()
        namelist = [x.strip() for x in namelist]
    return namelist

listObj = writeFileOnAList(filePath)

代码是正确的,但原因是错误的。Python确实通过引用传递,但传递对对象的引用,而不是对变量的引用。因此,当OP在其函数中有赋值时,它们只作用于局部变量。代码返回对对象的引用,赋值作用于函数外部的变量。公平点。在C++的意义上,它当然不能通过引用。也许通过对象传递会更清楚:即对象引用是通过值传递的。代码是正确的,但原因是错误的。Python确实通过引用传递,但传递对对象的引用,而不是对变量的引用。因此,当OP在其函数中有赋值时,它们只作用于局部变量。代码返回对对象的引用,赋值作用于函数外部的变量。公平点。在C++的意义上,它当然不能通过引用。也许通过对象传递会更清楚:即对象引用是通过值传递的。