Skip to content

Instantly share code, notes, and snippets.

@RDCH106
Created July 2, 2021 11:13
Show Gist options
  • Select an option

  • Save RDCH106/7c55ff4ad3cc77c156d5f0e514e5a2f6 to your computer and use it in GitHub Desktop.

Select an option

Save RDCH106/7c55ff4ad3cc77c156d5f0e514e5a2f6 to your computer and use it in GitHub Desktop.

Revisions

  1. RDCH106 created this gist Jul 2, 2021.
    45 changes: 45 additions & 0 deletions python3_requiere_named_arguments.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/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)")