The Idiocy of Python's Function Parameter Specification

By Xah Lee. Date: . Last updated: .

Language design. In python, you can define a function with unspecified number of unnamed keyword parameters, like this:

# -*- coding: utf-8 -*-
# python

# use 「**‹name›」 to receive unspecified number of keyword arguments
def ff(**ddd):
    # ddd is received as a dictionary
    return ddd

print ff( z = 8, c = 7, aa = 4, b = 3) # {'aa': 4, 'c': 7, 'z': 8, 'b': 3}

but, why'd anyone want to do that? No language i know of that lets one define, read:

UNSPECIFIED NUMBER of UNNAMED BUT NAMED parameters

But let's say we do need it. But in that case, just pass one dictionary!

wouldn't that be a simpler design with same power?

what's more, is that python also lets you define unspecified number of positional parameters, as well as keyword parameters, as well as any of all the above mixed.

But, is it simple as that? No. It gets very hairy. Each way you define them, your function receives the arg in a different way. You function may need to process a n-tuple data type, or hash table. Also, in the definition, the parameter needs to be in a particular order.

so, in summary, the syntax is complex, each case has a special form, and the semantics is complex, each case your function body need to process the args in a different way.

complexer and complexer.

really, positional param and keyword param is sufficient. When needed, programer can pass list or hash table. That really solves all problems, yet without introducing any complexity to the lang's syntax and semantics.

Perl and Ruby are both better here.

https://plus.google.com/+XahLee/posts/2yrSbkjuK2K discussion

Python Semantic Bad

  1. Why List Comprehension is Bad
  2. Python 3: Map with Side Effect Doesn't Work If Result is Not Used
  3. The Idiocy of Python's Function Parameter Specification
  4. Python Scope Complexity, Shallow Copy, Deep Copy, Circular List, and the Garbage Underneath Computer Languages
  5. Python: dict={} vs dict.clear()
  6. Python: Get Number of Arguments of Function
  7. Python: Copy Nested List, Shallow/Deep Copy
  8. Python: Keyword Argument Default Value Unstable
  9. Python: Append String in Loop
  10. FCK Python: String Methods, Functions, Slashes and Backslashes
  11. Python Bitwise Invert Oddity
  12. From Why Not Ruby to FCK Python, Hello Ruby
  13. Why Learn Lisp When There Are Perl and Python

If you have a question, put $5 at patreon and message me.