Some language proposals.

Jeff Epler jepler at unpythonic.net
Tue Feb 24 17:20:53 CET 2004


On Tue, Feb 24, 2004 at 03:53:42PM +0000, Antoon Pardon wrote:
> Now my idea would be to look at the local variables in
> a function like member variables of an object so that
> you could write
> 
> 
> def fun_1():
> 
>   a = some_value
>   b = a_value_too
> 
>   def fun_2():
> 
>     fun_1.a = new_value
>     fun_1.b = next_value

This is not compatible with existing code.  'fun_1.a = new_value'
already has a meaning (it creates or modifies an attribute on the
function object), so it can't be used to modify the locals() of a
particular invocation of fun_1.

> def fun_1():
> 
>   a = some_value
>   b = a_value_too
>       
>   def fun_2():
> 
>     from fun_1 import a, b
> 
>     a = new_value
>     b = next_value
> 
> 
> What do people think about this?
> As far as I know the proposal doesn't break existing
> code and seems in the spirit of python.

This is not in the sprit of Python.

This already has a meaning:
    from m import a
    a = 3
... it almost is the same as
    import m as _m
    a = m.a
    del _m
    a = 3
... which basically just sets a to 3 (it doesn't modify the module m)

> On a side note, what would people think about the idea of
> a from ... import ... statement usable on any object? So
> that we could do:

No, that won't do.  Consider the following Python program:
    sys = 64738
    from sys import exit
    exit()
The module name in 'import' and 'from ... import' doesn't refer to
objects in the current module's namespace, but in the namespace of
modules.

Anyway, because 'from .. import' is not magic, but just assignment,
there's no reason to prefer your proposed
    from A import a, b
to
    a, b = A.a, A.b
.. for instance, it can't make 'a = 3' change A.  It has a small
advantage that you don't need to type 'a' once on the left and once on
the right of '=', but that's about it.

Jeff




More information about the Python-list mailing list