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

Jeremy Banks jeremy at jeremybanks.ca
Thu Apr 23 10:59:23 EDT 2009


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.



More information about the Python-list mailing list