[Python-ideas] Add a feature similar to C++ "using some_namespace"

Ethan Furman ethan at stoneleaf.us
Mon Feb 7 22:07:06 CET 2011


Manuel Bärenz wrote:
[...]
> This could be easier to implement and read if we had something like:
> 
> class Spam(object):
>     def frobnicate(self):
>         using self:
>             eggs = buy_eggs()
>             scrambled = scramble(eggs)
>             return scrambled>  42
> 
> Of course this opens a lot of conceptual questions like how should this 
> using block behave if self doesn't have an attribute called "eggs", but 
> I think it is worth considering.

In Foxpro, the keyword is 'with' instead of 'using', and to make it 
clear when the to look into the namespace for the variable (instead of, 
for example, locals), the variable name is prepended with a '.'; so the 
example above becomes:

class Spam(object):
     def frobnicate(self):
         with self:
             .eggs = buy_eggs()
             .scrambled = scramble(eggs)
             return .scrambled > 42

While I have occasionally missed this feature, I haven't lost any sleep 
over it, either.

-1 without leading periods
+0 with leading periods

no preference on keyword name (with vs using vs ...)

~Ethan~



More information about the Python-ideas mailing list