From: Xah Lee on
just wrote this essay. Comment & feedback very welcome.

Python's Reference And Internal Model Of Computing Languages

Xah Lee, 2010-02-02

In Python, there are 2 ways to clear a hash: “myHash = {}” and
“myHash.clear()”. What is the difference?
↓

The difference is that “myHash={}” simply creates a new empty hash and
assigns to myHash, while “myHash.clear()” actually clear the hash the
myHash is pointing to.

What does that mean?? Here's a code that illustrates:

# python
# 2 ways to clear hash and their difference
aa = {'a':3, 'b':4}
bb = aa
aa = {}
print bb # prints {'a': 3, 'b': 4}

aa = {'a':3, 'b':4}
bb = aa
aa.clear()
print bb # prints {}

This is like this because of the “reference” concept. The opposite
alternative, is that everything is a copy, for example in most
functional langs. (with respect to programer-visible behavior, not how
it is implemented)

From the aspect of the relation of the language source code to the
program behavior by the source code, this “reference”-induced behavior
is similar to dynamic scope vs lexicol scope. The former being un-
intuitive and hard to understand the latter more intuitive and clear.
The former requires the lang to have a internal model of the language,
the latter more correspond to source code WYSIWYG. The former being
easy to implement and is in early langs, the latter being more popular
today.

As with many languages that have concept of references, or pointers,
this is a complexity that hampers programing progress. The concept of
using some sort of “reference” as part of the language is due to
implementation efficiency. Starting with C and others in 1960s up to
1990s. But as time went on, this concept in computer languages are
gradually disappearing, as they should.

Other langs that have this “reference” concept as ESSENTIAL part of
the semantic of the language, includes: C, C++, Java, Perl, Common
Lisp. (of course, each using different terminologies, and each lang
faction will gouge the other faction's eyes out about what is the true
meaning of terms like “reference”, “object”, “list/sequence/vector/
array/hash”, and absolutely pretend other meanings do not exist.
(partly due to, their ignorance of langs other than their own, partly,
due to male power struggle nature.))

Languages that do not have any “reference” or “object”, or otherwise
does not require the programer to have some internal model of source
code, are: Mathematica, JavaScript, PHP. (others may include TCL,
possibly assembly langs.)

For some detail on this, see: Jargons And High Level Languages and
Hardware Modeled Computer Languages.

---------------------------

(perm url of this essay can be found on my website.)

Xah
∑ http://xahlee.org/

☄
From: David Thole on
I read this....and am a tiny bit confused about the actual problem.

It's not exactly complex to realize that something like:
a = b = array
that a and b both point to the array.

Logically speaking, I'm not sure how one could assume that the same
assignment would yield a and b point to the same duplicate array. If
that was the case, why not do:
a = array..
b = array..

I know with what you were complaining about a few days ago, .clear makes
perfect sense. If a and b point to the same array, clear should clear
both arrays. Again, if you didn't want that to happen, create a
duplicate array.

Personally I feel that this complexity doesn't hamper programming
process, and yes while its good for efficiency it also just makes sense.

Also, I wouldn't look at PHP on the right way to do something
programming wise. I have ~5 years experience in this language, and I
dislike it a whole lot. There's a lot of things it should do right that
it doesn't out of convenience.

-David
www.thedarktrumpet.com

From: J�rgen Exner on
David Thole <dthole(a)gmail.com> wrote in comp.lang.perl.misc:
>I read this....and am a tiny bit confused about the actual problem.
>
>It's not exactly complex to realize that something like:
>a = b = array
>that a and b both point to the array.

???
What are you talking about? First of all you should post actual code,
not pseudo-code because pseudo-code leads to misunderstandings. Did you
mean
@a = @b = @array

Second what do you mean by "pointing"? That is a very ambiguous term. if
you do the assignment as written above then @a and @b will be _copies_
of @array. If you want two additional references to the same array
insted then you have to create that reference first and assign that
reference to $a and $b instead of copying the array, see "perldoc
perlref" for details. And remember, references are scalars, no matter if
they reference other scalars or arrays.

>Logically speaking, I'm not sure how one could assume that the same
>assignment would yield a and b point to the same duplicate array. If

Now what? The same array or a duplicate array? Two very different and
mutually exclusive things.

>that was the case, why not do:
>a = array..
>b = array..

Which, after correcting the obvious syntax errors is the same as the
code above.

>I know with what you were complaining about a few days ago, .clear makes
>perfect sense. If a and b point to the same array, clear should clear

They don't point, they are copies. And what do you mean by "clear"?

>both arrays. Again, if you didn't want that to happen, create a
>duplicate array.

But that is what that code above does. If you want references then
create references.

jue
From: Tad McClellan on
["Followup-To:" header set to comp.lang.perl.misc.]

Jürgen Exner <jurgenex(a)hotmail.com> wrote:
> David Thole <dthole(a)gmail.com> wrote in comp.lang.perl.misc:
>>I read this....and am a tiny bit confused about the actual problem.
>>
>>It's not exactly complex to realize that something like:
>>a = b = array
>>that a and b both point to the array.
>
> ???
> What are you talking about? First of all you should post actual code,


First of all, we should not be feeding the troll!

actual code in one of Perl, Python or Lisp?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
From: David Thole on
Tad,

I can promise you I'm not trolling - I was responding to Xah Lee's
original newsgroup post. I'll leave it as that.

Jurgen:

I avoided posting actual code because Xah Lee's post was cross-newgroup,
which if I write something in Lisp, it definitely wouldn't be applicable
to Perl or Python. It was a decision to keep it simple is all, language
inspecific.

For the second point, yes "pointing" is an ambiguous term in this case.
As far as your justification, please don't bother. I was responding to
an original post in which justifying the logic, such like in Ruby:

a = b = {"a" => 5, "b" => 10}
a
=> {"a" => 5, "b" => 10}
b
=> {"a" => 5, "b" => 10}
a["a"] => 15
=> 15
a["a"] => 15
=>15
a
=> {"a" => 15, "b" => 10}
b
=> {"a" => 15, "b" => 10}

That this makes sense. That the location of the hash in memory is
pointed to by the same two variables, a and b respectively. The
original argument from Xah Lee appeared, at least to me, that a would
contain a separate copy of the has that b contains - in which when I set
a["a"] = 15, that b["a"] would still be 5.

I can't speak much to perl. His argument was that something along the
lines of: a.clear() [Note: switching now to python syntax sudden,
sorry..] sets both a = {} and b = {}, which he felt was wrong, and that
a should be {} while b should still be {"a" => 5, "b" => 10}.

As I tested in my very lacking understanding of Perl, it appears to do
the same thing that Python, Ruby and Lisp do. Which is good in my
opinion, I think you're fighting against the wrong individual....

So...as I am clarifying once again, I'm not a troll and I'm not saying
anything is wrong. My feeling is that the fact that a and b reference
the same memory space for this hash is *ok* not an issue such as the
original post made it feel.

I apologize for cross posting back to lisp, it's so I can see responses
later if they come in.

-David
http://www.thedarktrumpet.com