简体   繁体   English

在python中使用两种不同类型的参数调用函数

[英]Calling function with two different types of arguments in python

I am new to Python. 我是Python的新手。 I came across a weird case and am not able to figure out what the issue is. 我遇到了一个奇怪的案例,无法弄清楚问题出在哪里。 I have 2 versions of a function written in Python :- 我有2个用Python编写的函数版本:-

v1 - v1-

def fileLookUp(fixedPath, version):
    if version:
        targetFile="c:\\null\\patchedFile.txt"
    else:
        targetFile="c:\\null\\vulFile.txt"

#some more code follows

and v2 - 和v2-

def fileLookUp(fixedPath, version):
    if version:
        print "ok"
    else:
        print "not ok"

#some more code follows

where the parameter fixedPath is a string that is entered and the parameter version is supposed to be an integer value. 其中,参数fixedPath是输入的字符串,参数版本应该是整数值。 The 1st function (v1) does not work as expected, while tje second works perfectly. 第一个功能(v1)无法正常工作,而第二个功能则完美运行。 Both the times the function is called as fileLookUp("c:\\\\dir\\\\dir\\\\", 1) . 两次都将该函数称为fileLookUp("c:\\\\dir\\\\dir\\\\", 1)

In the 1st case the error received is :- 在第一种情况下,收到的错误是:

fileLookUp("D:\\Celine\\assetSERV\\", 1)
Exception: fileLookUp() takes exactly 2 arguments (1 given)

Please let me know why is the 1st function throwing the exception? 请让我知道为什么第一个函数引发异常?

Here is the actual code.... 这是实际的代码。

from System.IO import *;

def fileLookUp(fixedPath, version):
if version:
    targetFile="c:\\null\\patchedFile.txt";
else:
    targetFile="c:\\null\\vulFile.txt";
vulFileHandle=open(targetFile,"a+");
temp=fixedPath;
if not Directory.GetDirectories(fixedPath):
    files=Directory.GetFiles(fixedPath);
    for eachFile in files:
        print eachFile;
        hash = Tools.MD5(eachFile);
        print hash;
        vulFileHandle.write(eachFile+'\t'+hash+'\n');
else:
    directory=Directory.GetDirectories(fixedPath);
    for folder in directory:
        if vulFileHandle.closed:
            vulFileHandle=open(targetFile,"a+");
        fixedPath="";
        fixedPath+=folder;
        fixedPath+="\\";
        vulFileHandle.close();
        fileLookUp(fixedPath);
    filess=Directory.GetFiles(temp);
    for eachFilee in filess:
        if vulFileHandle.closed:
            vulFileHandle=open(targetFile,"a+");
        print eachFilee;
        hashh = Tools.MD5(eachFilee);
        print hashh;
        vulFileHandle.write(eachFilee+'\t'+hashh+'\n');
if not vulFileHandle.closed:
    vulFileHandle.close();

it is simply a recursive code to print out the hash of all files in a directory. 只是打印出目录中所有文件的哈希值的递归代码。

You have a call "fileLookUp(fixedPath);" 您有一个呼叫“ fileLookUp(fixedPath);”。 around line 26 or so (just counted roughly) with only one argument sent in. Your definition doesn't allow that. 大约在第26行左右(仅大致计数),仅发送了一个参数。您的定义不允许这样做。 Send in the version in this call, or give a default value to version in the definition. 发送此调用中的版本,或为定义中的版本提供默认值。

The way these functions are written, both of them must be called with two arguments. 这些函数的编写方式,必须使用两个参数来调用它们。 The error message you're getting indicates that one of them is being called with only one argument. 您收到的错误消息表明仅使用一个参数就调用了其中一个。 In Python, if you want to make an argument optional, you have to explicitly state what value the optional argument should have if it is not provided. 在Python中,如果要将参数设为可选,则必须明确说明可选参数(如果未提供)应具有的值。 For example, since version is to be an int , and you test it with if version: , a good default value could be 0. You could also use None . 例如,由于version将是一个int ,并且您使用if version:对其进行了测试,因此一个很好的默认值可以为0。您还可以使用None

def fileLookUp(fixedPath, version=0):
    # etc.

def fileLookUp(fixedPath, version=None):
    # etc.

If 0 is a valid version number, and you want to test for whether a value was actually passed, use the second and test against None specifically: 如果0是有效的版本号,并且您要测试是否实际传递了值,请使用第二个值并针对None测试:

def fileLookUp(fixedPath, version=None):
    if version is None:
        # etc.

在(格式很差的)完整fileLookUp ,您要递归调用fileLookUp 而不传递version参数。

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

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