The motivation came up in this thread on python-forum.org
http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=11606
My main motivation most likely is just that it would open up some very interesting meta-programing or dynamic programming opportunities, variables that exists entirely programmatically without the prefix of object_instance.foo, surely just syntactical sugar to some (or even most) - I have found it to be valuable syntax. just like the current with statement bypasses some syntactical nusances of try: except: .. this would bypass the nusance of inst.x inst.y inst.z ...
After posting last night I realized A pitfall of implementing this - would be
interrogate(foo, bar):
xylophone = 1024
using this reference - ..
http://docs.python.org/reference/datamodel.html#customizing-attribute-access
__setattr__ has no current way to fall through, so foo would get the setattr event, and not be able to allow interrogate to hand it to bar.
I posit that raising the AttributeError exception would be sufficient, for the hand off to occur, and if foo, then bar both did it, then one of two ways to handle it would be the current scope variables would get it, (eg it would be __del__ after the end of the block) or putting the new variable in the parent namespace, since it is somewhat assumed that locals would follow after bar
On Fri, 14 Aug 2009 04:22:07 pm Prozacgod wrote:What is the motivation? Is it just to reduce typing?
> A while back i had learned about the 'with' statement and at first I
> naively thought that it worked similarly to a with statement I was
> familiar with, from delphi/pascal - obviously it doesn't and I was
> instantly hit with the idea of how useful it would be to have a
> keyword that could use a namespace from an object, hitting the
> familiar __getattr__ functions and related.
Being an ex-Pascal coder myself, I originally missed having a
Pascal-like 'with' statement too. But now I'm not so sure it is needed.
I know this proposal (under the name 'with') has been discussed before,
but I can't find a PEP for it... ah, here you go, it was in the FAQs:
http://www.python.org/doc/faq/general/#why-doesn-t-python-have-a-with-statement-for-attribute-assignments
C-# doesn't have one either:
http://msdn.microsoft.com/en-us/vcsharp/aa336816.aspx
I don't think there are any problems with a Pascal-style 'with'
statement that couldn't be overcome, but I don't think the benefit is
great enough to create a new keyword for it. Can you explain in more
detail why this proposed feature is useful?
--
Steven D'Aprano
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
http://mail.python.org/mailman/listinfo/python-ideas