Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python's Reference And Internal Model Of Computing Languages

1 view
Skip to first unread message

Xah Lee

unread,
Feb 2, 2010, 8:28:10 PM2/2/10
to
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/


David Thole

unread,
Feb 5, 2010, 10:53:33 AM2/5/10
to
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

J�rgen Exner

unread,
Feb 5, 2010, 11:48:29 AM2/5/10
to
David Thole <dth...@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

Tad McClellan

unread,
Feb 5, 2010, 12:25:34 PM2/5/10
to
["Followup-To:" header set to comp.lang.perl.misc.]

Jürgen Exner <jurg...@hotmail.com> wrote:
> David Thole <dth...@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/"

J�rgen Exner

unread,
Feb 5, 2010, 12:34:54 PM2/5/10
to
Tad McClellan <ta...@seesig.invalid> wrote:
>["Followup-To:" header set to comp.lang.perl.misc.]
>
>J�rgen Exner <jurg...@hotmail.com> wrote:
>> David Thole <dth...@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!

Oh, is David Thole another incarnation of Xha Lee? I haven't seen his
name before...

jue

David Thole

unread,
Feb 5, 2010, 1:19:27 PM2/5/10
to
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

J�rgen Exner

unread,
Feb 5, 2010, 1:48:16 PM2/5/10
to
David Thole <dth...@gmail.com> wrote:
>I can promise you I'm not trolling - I was responding to Xah Lee's
>original newsgroup post. I'll leave it as that.

Ahh, that explains a lot. Any sensible person has killfiled Xah Lee ages
ago, therefore his post never showed up. And you didn't cite or
reference any preceeding posting, therefore I assumed yours was an
original and not a response to some insane troll.

jue

Tad McClellan

unread,
Feb 5, 2010, 2:03:12 PM2/5/10
to
["Followup-To:" header set to comp.lang.perl.misc.]

David Thole <dth...@gmail.com> wrote:
> Tad,
>
> I can promise you I'm not trolling -


I did not say, or even imply, that you were trolling.


> I was responding to Xah Lee's
> original newsgroup post.


I did say that you were feeding the troll.

David Thole

unread,
Feb 5, 2010, 3:39:12 PM2/5/10
to
Yeah, I'm relatively new to Usenet so far...maybe 2 weeks old, so I have
only begun to realize that he's actually a troll. How to killify him is
something I'm not sure of yet...but seeing some spam get through, plus
stuff like this makes me want to check up on how to do it. I also need
to figure out how to make my client include previous posts, quoted at
least somewhat. heh, using gnus...so I'm sure it's possible to get this
configured right.

-David
http://www.thedarktrumpet.com

David Thole

unread,
Feb 5, 2010, 3:47:23 PM2/5/10
to
Tad McClellan <ta...@seesig.invalid> writes:

> ["Followup-To:" header set to comp.lang.perl.misc.]
>
> David Thole <dth...@gmail.com> wrote:
>> Tad,
>>
>> I can promise you I'm not trolling -
>
>
> I did not say, or even imply, that you were trolling.
>

My apologies, I misinterpreted what you were saying.

>
>> I was responding to Xah Lee's
>> original newsgroup post.
>
>
> I did say that you were feeding the troll.

Yeah, I'm kinda getting that now. Sorry for the intrusion for all the
people who read this on perl/cl

-David

Steven D'Aprano

unread,
Feb 5, 2010, 5:26:03 PM2/5/10
to
On Fri, 05 Feb 2010 09:53:33 -0600, David Thole wrote:

> 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.

It's an easy mistake to make, if you don't understand Python's object
model:


>>> a = b = 2
>>> a += 1
>>> a, b
(3, 2)
>>> a = b = [2]
>>> a[0] += 1
>>> a, b
([3], [3])

For the non-Python coders reading, the difference is that ints are
immutable, and hence a += 1 assigns a to a new int, leaving b untouched,
but lists are mutable, hence a[0] += 1 mutates the list which both a and
b refer to. This is a Good Thing, but it is one of the things which give
newcomers some trouble.


--
Steven

Pascal J. Bourguignon

unread,
Feb 5, 2010, 5:57:09 PM2/5/10
to
David Thole <dth...@gmail.com> writes:

gnus, easy: just type M-x gnus-kill-file-kill-by-author RET on the
article of the author you want to kill-file.

--
__Pascal Bourguignon__

John Bokma

unread,
Feb 5, 2010, 5:59:14 PM2/5/10
to
David Thole <dth...@gmail.com> writes:

F (shift F key) = follow up with quoting, no configuring needed (at
least not in my experience).
Regarding scoring, see:
http://www.gnu.org/software/emacs/manual/html_node/gnus/Scoring.html#Scoring
(killfiles have been replaced by scoring).

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development

David Thole

unread,
Feb 5, 2010, 8:14:44 PM2/5/10
to
Thanks,

Another person who read also contacted me directly by email to help me
with the kill file. I think I got it working and all, so I'll know
within a few days if it worked or not :)

Thanks though.

David
http://www.thedarktrumpet.com

John Bokma

unread,
Feb 5, 2010, 9:18:42 PM2/5/10
to
David Thole <dth...@gmail.com> writes:

> Thanks,
>
> Another person who read also contacted me directly by email to help me
> with the kill file. I think I got it working and all, so I'll know
> within a few days if it worked or not :)
>
> Thanks though.

In order to have a real signature:

create a .signature file in your home directory and copy this in it
(without the 8<---8<---8<---8<---8<---)

8<---8<---8<---8<---8<---
David
http://www.thedarktrumpet.com/
8<---8<---8<---8<---8<---

And modify (setq gnus-posting-styles ...) in your .gnus.el to:

(setq gnus-posting-styles
'((".*"
(signature-file "~/.signature")
(x-url "http://www.thedarktrumpet.com/")
(organization "The Dark Trumpet"))))

(A real signature has a special marker consisting of -- followed by
exactly one space)

Peter J. Holzer

unread,
Feb 6, 2010, 4:57:54 AM2/6/10
to
On 2010-02-05 18:19, David Thole <dth...@gmail.com> wrote:
> 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.

This is a noble goal, but "simple" and "language inspecific" don't go
well together. Apparently simple terms like "array" have slightly
different semantics in different languages, so to be language inspecific
you need to carefully define all these terms before you use them. If you
just write something like

a = b

without defining exactly what a and b are and what = does, everybody has
to guess and it is very likely that different people will guess
differently.

hp

David Thole

unread,
Feb 6, 2010, 8:17:35 AM2/6/10
to
John Bokma <jo...@castleamber.com> writes:

> In order to have a real signature:

> ...


> (A real signature has a special marker consisting of -- followed by
> exactly one space)

Thanks for the signature suggestion, this'll save some typing and looks
better than the manual typing of this I have been doing.

--
David
http://www.thedarktrumpet.com/

John Bokma

unread,
Feb 6, 2010, 12:08:14 PM2/6/10
to
David Thole <dth...@gmail.com> writes:

> John Bokma <jo...@castleamber.com> writes:
>
>> In order to have a real signature:
>> ...
>> (A real signature has a special marker consisting of -- followed by
>> exactly one space)
>
> Thanks for the signature suggestion, this'll save some typing and looks
> better than the manual typing of this I have been doing.

You're welcome ;-) On top of that, a signature preceded with this
special marker gives usenet clients (including Gnus ) the option to
automatically remove the signature when replying.

Giorgos Keramidas

unread,
Feb 6, 2010, 10:33:57 PM2/6/10
to

Hi David,

I don't know which one of the two groups you are reading more often,
hence the cross-post. Please check the difference between 'f' and 'F'
in the Gnus manual. The later quotes everything, and you get to strip
the bits yourself.

HTH,
Giorgos

0 new messages