Python NameError:名称未定义(与具有默认输入参数类型有关) [英] Python NameError: name is not defined (related to having default input argument types)

查看:57
本文介绍了Python NameError:名称未定义(与具有默认输入参数类型有关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我要声明的函数的输入参数中调用 len(myByteArray)时遇到问题.我希望将其作为默认参数,但Python似乎不喜欢它. myByteArray 的类型为 bytearray .请参阅此处有关字节数组的文档.我正在访问其内置的 find 函数此处记录(请参见"bytes.find").

I have a problem with the fact that I am calling len(myByteArray) in the input arguments to a function I am declaring. I'd like that to be a default argument, but Python doesn't seem to like it. myByteArray is of type bytearray. See documentation on bytearray here. I am accessing its built-in find function, documented here (see "bytes.find").

我的功能:

def circularFind(myByteArray, searchVal, start=0, end=len(myByteArray)):
    """
    Return the first-encountered index in bytearray where searchVal 
    is found, searching to the right, in incrementing-index order, and
    wrapping over the top and back to the beginning if index end < 
    index start
    """
    if (end >= start):
        return myByteArray.find(searchVal, start, end)
    else: #end < start, so search to highest index in bytearray, and then wrap around and search to "end" if nothing was found 
        index = myByteArray.find(searchVal, start, len(myByteArray))
        if (index == -1):
            #if searchVal not found yet, wrap around and keep searching 
            index = myByteArray.find(searchVal, 0, end)
        return index 

尝试使用上述功能的示例:

Examples to attempt to use the function above:

#-------------------------------------------------------------------
#EXAMPLES:
#-------------------------------------------------------------------
#Only executute this block of code if running this module directly,
#*not* if importing it
#-see here: http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm
if __name__ == "__main__": #if running this module as a stand-alone program
    import random
    random.seed(0)

    bytes = bytearray(100)
    for i in range(len(bytes)):
        bytes[i] = int(random.random()*256)

    print(list(bytes)); print();

    print('built-in method:')
    print(bytes.find(255))
    print(bytes.find(2,10,97))
    print(bytes.find(5,97,4))

    print('\ncircularFind:')
    print(circularFind(bytes,255))
    print(circularFind(bytes,2,10,97))
    print(circularFind(bytes,5,97,4))

错误:

NameError:名称'myByteArray'未定义

NameError: name 'myByteArray' is not defined

但是,如果我只删除默认参数( = 0 = len(myByteArray)),它可以正常工作.但是...我真的想要那些默认参数,以便 start end 参数是可选的.我该怎么办?

If I just remove my default arguments (=0 and =len(myByteArray)), however, it works fine. But...I really want those default arguments there so that the start and end arguments are optional. What do I do?

在C ++中,这很容易,因为在编写函数时指定了参数类型.

In C++ this would be easy, as argument types are specified when you write functions.

推荐答案

定义函数时,将评估Python默认参数.相反,您想要这样的东西:

Python default arguments are evaluated when the function is defined. Rather, you want something like this:

def circularFind(myByteArray, searchVal, start=0, end=None):
    """
    Return the first-encountered index in bytearray where searchVal 
    is found, searching to the right, in incrementing-index order, and
    wrapping over the top and back to the beginning if index end < 
    index start
    """
    if end is None:
        end = len(myByteArray)
    # continue doing what you were doing

这篇关于Python NameError:名称未定义(与具有默认输入参数类型有关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆