[Python-ideas] Definition Symmetry

Ned Batchelder ned at nedbatchelder.com
Thu Jan 31 19:11:55 CET 2013


On 1/31/2013 12:15 PM, MRAB wrote:
> On 2013-01-31 16:35, Jason Keene wrote:
>> Why do function definitions require parens?
>>
>>>>> class MyClass:
>> ...     pass
>> ...
>>>>> def my_func:
>>    File "<stdin>", line 1
>>      def my_func:
>>                 ^
>> SyntaxError: invalid syntax
>>
>> This seems to me to break a symmetry with class definitions.  I assume
>> this is just a hold off from C, perhaps there is a non-historical reason
>> tho.
>>
>> I believe in the past we've forced parens in list comprehensions to
>> create a symmetry between comprehensions/generator expressions. Why not
>> for this?
>>
> The parentheses are always required when calling the function, so it
> makes sense to always require them when defining the function.
>
> The case with class definitions is different; they are used in the
> definition only when you want to specify the superclass.
>

I think parens for super class are an unfortunate syntax, since it looks 
just like arguments to the class and is confusing for some beginners:

     def function(arg):
         ...
     function(10)            # Similar syntax: 10 corresponds to arg

     class Thing(Something):
         ...
     thing = Thing(10)    # How does 10 relate to Something? It doesn't.

A better syntax (which I AM NOT PROPOSING) would be:

     class Thing from Something:

--Ned.

> They are always required when creating an instance of the class and in
> method definitions.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>




More information about the Python-ideas mailing list