Is behavior of += intentional for int?

Albert Hopkins marduk at letterboxes.org
Sun Aug 30 10:12:26 EDT 2009


On Sun, 2009-08-30 at 04:49 -0700, Carl Banks wrote:
> It's pretty common for people coming from "name is a location in
> memory" languages to have this conception of integers as an
> intermediate stage of learning Python's object system.  Even once
> they've understood "everything is an object" and "names are references
> to objects" they won't have learned all the nuances of the system, and
> might still (not unreasonably) think integer objects could be mutable.
> 
I agree.  Python (and other similar languages?) are different in that.
'x' does not point to an area in memory, where you can do anything with
that area.  But in Python there are objects, and they are "references"
in memory that some magical "reference counter" keeps track of for us
(and that's a wonderful thing).  And what is 'x'?  Well 'x' is just some
"label" that just so happens to have the privelage of being associated
with this "unnamed" object.  'x' could just as easily associate itself
with another object.

I think that the Blue programming language, which I have been looking at
lately, makes this distinction even clearer.  For example, functions are
not defined by names at all.  Instead of

    def funcname(): ...

You have

    func{...};

If you actually want to be able to reference the function later (as you
probably would) then it's just a simple assignment just like any other
assignment:

    funcname = func{...};

But i think it makes it more clear that "funcname" just so happens to
reference this object that's a function.  It's the same basic philosophy
when applied to methods:

    MyClass = sys.class();
    MyClass.my_method = func{...};

Blue also has interesting, simple rules wrt scopes.  It's a surprisingly
small, simple language (yet in a very early stage of development.

> However, it'd be nice if all these people didn't post here whining
> about how surprising and unintuitive it is and instead just said, "ah,
> integers are immutable, got it", quietly to themselves.

Yes, when I was first learning Python, at least the book I used made it
very clear when introducing a new type to specify that type as mutable
or immutable.  It's a very core concept to Python.  If you choose to
ignore it or refuse to understand it then you are asking for trouble.

-a





More information about the Python-list mailing list