Created
July 2, 2021 11:13
-
-
Save RDCH106/7c55ff4ad3cc77c156d5f0e514e5a2f6 to your computer and use it in GitHub Desktop.
Requiring your arguments be named in a function calling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def classic_function(x, y ,z): | |
print(x, y, z) | |
def force_named_arguments_function(*, x, y ,z): | |
print(x, y, z) | |
if __name__ == "__main__": | |
print("Classic function calling") | |
print("================================================\n") | |
classic_function(1, 2, 3) | |
classic_function(1, 2, z=3) | |
classic_function(x=1, y=2, z=3) | |
try: | |
eval('classic_function(x=1, 2, 3)') | |
except SyntaxError as e: | |
print(e) | |
print("Expected error: classic_function(x=1, 2, 3)") | |
# -------------------- | |
print("\nFunction calling with required named arguments") | |
print("================================================\n") | |
try: | |
eval('force_named_arguments_function(x=1, 2, 3)') | |
except SyntaxError as e: | |
print(e) | |
print("Expected error: force_named_arguments_function(x=1, 2, 3)") | |
try: | |
eval('force_named_arguments_function(1, 2, z=3)') | |
except TypeError as e: | |
print(e) | |
print("Expected error: force_named_arguments_function(1, 2, z=3)") | |
force_named_arguments_function(x=1, y=2, z=3) | |
try: | |
eval('force_named_arguments_function(x=1, 2, 3)') | |
except SyntaxError as e: | |
print(e) | |
print("Expected error: force_named_arguments_function(x=1, 2, 3)") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment