Why can function definitions only use identifiers, and not attribute references or any other primaries?

Gary Herron gherron at islandtraining.com
Thu Apr 23 17:18:42 CEST 2009


Jeremy Banks wrote:
> Thanks for your comments.
>
> On Thu, Apr 23, 2009 at 11:52, Gary Herron <gherron at islandtraining.com> wrote:
>   
>>> [...]
>>>       
>> There's no need for a specific addition to the syntax to do this.
>>
>> Try this:
>>
>>   def foo_bar():
>>       return(...)
>>   foo.bar = foo_bar
>>
>>     
>>> [...]
>>>       
>> and this:
>>
>>   def foo_bar():
>>       return(...)
>>   foo["bar"] = foo_bar
>>
>>     
>
> I understand that this is possible now, but I just don't see why it's
> necessary to do it at all.
>
>   
>>> In this case I could see there being a problem in that there's nothing
>>> to get the function's __name__ from, but that doesn't apply for the
>>> first example.
>>>
>>>       
>> Not sure what you mean here.
>>     
>
> 	>>> def foo(): pass
> 	...
> 	>>> bar = foo
> 	>>> bar.__name__
> 	'foo'
> 	>>>
>
> If I defined foo.bar it would know that the method name was "bar", but
> if I defined foo["bar"] there's be no clear identifier to use for the
> function's name. I don't see this as a great problem, since anonymous
> functions already exist, but I thought it was worth acknowledging.
>
> To be clear, I don't see this as a serious fault in the language, but
> as an unnecessary restriction that makes code a little less direct
> than it could be.
>   


Things like your suggestion are called "syntactic-sugar"  -- syntax that 
adds a convenience, but *no* new functionality.  Python has plenty of 
"syntactic-sugar"s, and more will be added in the future.  To make an 
argument for such an addition, one would have to describe some 
compelling (and general) use cases in a well-argued PEP.  You're welcome 
to try, but be forewarned, most PEP's (especially syntax changing PEPs) 
don't fly far.


> --
> http://mail.python.org/mailman/listinfo/python-list
>   




More information about the Python-list mailing list