Question about math.pi is mutable

Ben Finney ben+python at benfinney.id.au
Sun Nov 8 17:54:20 EST 2015


BartC <bc at freeuk.com> writes:

> On 08/11/2015 21:00, Ben Finney wrote:
> > The namespace can change dynamically, which is another way of what
> > people have been trying to tell you all through this thread.
> >
> > The compiler *cannot* know what the names will be at every single
> > point they'll be looked un in the namespace dictionary. This
> > dynamism of each namespace is a Python feature.
>
> Suppose this is the python program:
>
> import m
> a=10
> b=20
> c=30
> m.f()
>
> The set of global names the compiler knows will be ("m","a","b","c").
>
> I don't believe code can remove these names (that would cause
> problems).

It may indeed cause problems. Those names can nevertheless be changed.

Names at module level are simply attributes on the module object. The
module can gain attributes, lose attributes, and its attributes can
chage; its namespace is just as mutable as any other object's namespace.

> You say the set of global names can be added to - after this lot, but
> I can't see that the first four are going to change.

You have not yet learned enough about the Python data model:

    A module object has a namespace implemented by a dictionary object
    (this is the dictionary referenced by the __globals__ attribute of
    functions defined in the module). Attribute references are
    translated to lookups in this dictionary, e.g., m.x is equivalent
    to m.__dict__["x"]. A module object does not contain the code
    object used to initialize the module (since it isn’t needed once
    the initialization is done).

    <URL:https://docs.python.org/3/reference/datamodel.html>

Any name can be deleted, just like any other reference, with the ‘del’
statement.

Any reference (including names) can be added or changed in a module
namespace like any other object's namespace.

> Attributes are harder because they are more of a free-for-all

Please understand that a module *is* an object, and a name in a module's
namespace *is* an attribute on that module.

Also, please *learn about* the way Python works; you have certainly been
here long enough to know how to look up answers in the documentation
instead of making ignorant assertions here.

-- 
 \       “What I resent is that the range of your vision should be the |
  `\                                 limit of my action.” —Henry James |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list