简体   繁体   English

为什么内置函数在 python 中没有被归类为可调用的,我怎样才能让它们在我的程序中显示为可调用的?

[英]Why aren't builtin functions classified as callable in python and how can I make them appear callable in my program?

print(callable(chr(97)))
print(callable(ord("a")))

I was surprised to see that these lines of code both printed false.我很惊讶地看到这些代码行都打印出了错误。 It doesn't make intuitive sense that builtin functions, such as chr or ord are technically not callable, since I can literally call them anywhere in a program.内置函数(如 chr 或 ord)在技术上不可调用,这没有直观意义,因为我可以在程序中的任何地方调用它们。 How can I make these builtin functions appear callable in my program without redefining them?如何在不重新定义它们的情况下使这些内置函数在我的程序中显示为可调用? I'm receiving the errors below in my college's autograder:我在大学的自动评分器中收到以下错误:

exercises/ex03/cipher.py:9: error: Function "builtins.chr" is not valid as a type exercises/ex03/cipher.py:9: note: Perhaps you need "Callable[...]" or a callback protocol?练习/ex03/cipher.py:9:错误:函数“builtins.chr”作为类型无效练习/ex03/cipher.py:9:注意:也许您需要“Callable[...]”或回调协议? exercises/ex03/cipher.py:11: error: Function "builtins.chr" is not valid as a type exercises/ex03/cipher.py:11: note: Perhaps you need "Callable[...]" or a callback protocol?练习/ex03/cipher.py:11:错误:函数“builtins.chr”作为类型无效练习/ex03/cipher.py:11:注意:也许您需要“Callable[...]”或回调协议? exercises/ex03/cipher.py:12: error: chr?练习/ex03/cipher.py:12:错误:chr? has no attribute "islower" exercises/ex03/cipher.py:32: error: Function "builtins.chr" is not valid as a type exercises/ex03/cipher.py:32: note: Perhaps you need "Callable[...]" or a callback protocol?没有属性“islower”练习/ex03/cipher.py:32:错误:函数“builtins.chr”作为类型无效练习/ex03/cipher.py:32:注意:也许你需要“Callable[.. .]”或回调协议?

"""Caesar Cipher encoding and decoding program."""

UPPER_A: int = ord("A")
UPPER_Z: int = ord("Z")
LOWER_A: int = ord("a")
LOWER_Z: int = ord("z")


def encode_char(input: chr) -> chr:
    """Encode character function."""
    output: chr = ""
    if input.islower() is True:
        output = chr((ord(input) - LOWER_A + 1) % 26 + LOWER_A)
    else:
        output = chr((ord(input) - UPPER_A + 1) % 26 + UPPER_A)
    return output


def encode_str(input: str) -> str:
    """Encode string function."""
    output: str = ""
    c: int = 0
    while (c < len(input)):
        if input[c].islower() is True:
            output += chr((ord(input[c]) - LOWER_A + 1) % 26 + LOWER_A)
        else:
            output += chr((ord(input[c]) - UPPER_A + 1) % 26 + UPPER_A)
        c += 1
    return output


def decode_char(input: chr) -> chr:
    """Decode character function."""
    output: str = ""
    if input.islower() is True:
        output = chr(LOWER_Z - (LOWER_Z - ord(input) + 1) % 26)
    else:
        output = chr(UPPER_Z - (UPPER_Z - ord(input) + 1) % 26)
    return output


def decode_str(input: str) -> str:
    """Decode string function."""
    output: str = ""
    c: int = 0
    while (c < len(input)):
        if input[c].islower() is True:
            output += chr(LOWER_Z - (LOWER_Z - ord(input[c]) + 1) % 26)
        else:
            output += chr(UPPER_Z - (UPPER_Z - ord(input[c]) + 1) % 26)
        c += 1
    return output

This is my actual Caesar Cipher program.这是我实际的凯撒密码程序。

Your line:您的线路:

print(callable(chr(97)))

is actually calling the built-in chr() with the parameter 97 .实际上是用参数97 calling内置的chr() This call returns a string instance, which is being passed to callable() .此调用返回一个string实例,该实例被传递给callable() So an instance of a string is NOT callable.所以一个string的实例是不可调用的。

chr(97) evaluates to 'a' . chr(97)计算结果为'a' So you are effectively writing callable('a') , which is false.所以你正在有效地编写callable('a') ,这是错误的。 If you had tried callable(chr) , it would be true since chr is a callable function.如果您尝试过callable(chr) ,那将是正确的,因为chr是一个可调用函数。

You are getting an error because you are trying to use chr as a type when there is no such type in Python.您收到一个错误,因为当 Python 中没有这种类型时,您试图将chr用作类型。 Replace all instances of chr in your code where it is used as a type to str and it'll be fine.替换代码中chr所有实例,将其用作str的类型,就可以了。

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

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