Skip to content

Instantly share code, notes, and snippets.

@RDCH106
Created July 2, 2021 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RDCH106/7c55ff4ad3cc77c156d5f0e514e5a2f6 to your computer and use it in GitHub Desktop.
Save RDCH106/7c55ff4ad3cc77c156d5f0e514e5a2f6 to your computer and use it in GitHub Desktop.
Requiring your arguments be named in a function calling
#!/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