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. 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? 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? exercises/ex03/cipher.py:12: error: 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?
"""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
. This call returns a string
instance, which is being passed to callable()
. So an instance of a string
is NOT callable.
chr(97)
evaluates to 'a'
. So you are effectively writing callable('a')
, which is false. If you had tried callable(chr)
, it would be true since chr
is a callable function.
You are getting an error because you are trying to use chr
as a type when there is no such type in Python. Replace all instances of chr
in your code where it is used as a type to str
and it'll be fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.