Re: [Edu-sig] Python for Beginners
Right, as long as by "zero names" you're ruling out any pointers to pointers through a data structure, ...
Are you referring to this situation: IDLE 1.2.1
import sys a = 'this is a really unique value' sys.getrefcount(a) 2 dict01 = { 'first' : a, 'second' : 2 } sys.getrefcount(a) 3 dict02 = { 'uno' : a, 'dos' : 222 } sys.getrefcount(a) 4
... which are anonymous from another angle (the structure itself has a name, but the objects may be like Rod(color="Red"), with color choosing the length behind the scenes. d = {"red":Rod()} is then a string literal mapped to an object, *like* a top-level name, but not. d is the top-level name in this namespace. Rod( ) in a sense has no name, is a call to a constructor that returned on object captured within a dictionary. So it won't be garbage collected, as it knows there's this pointer for some reason (for what reason the object will have no clue).
"Rod() in a sense has no name" ?? Doesn't the Rod object get the name "red" in *almost exactly* the same way in these two cases? Case #1: >>> red = Rod() Case #2: >>> d = { "red" : Rod() } In Case #1, the name "red" is created in the local (or maybe global) namespace. In Case #2, the name "red" is created in the namespace of a dictionary object. In the sticky-note metaphor: a dictionary object is just a collection of sticky-notes -John Posner
<< SNIP >>
a dictionary object is just a collection of sticky-notes
-John Posner
One program I've been meaning to write, but haven't gotten to yet, creates arbitrarily deep, legal data structures, with "easter egg" or "treasure" randomly buried. The output would be such as: puzzle = {1:[{},([],'a')],(0,):["X"]} -- except it might go on for several pages of nesting. In this case, the object is to name 'X' and the solution is easy:
puzzle[(0,)][0] 'X'
One point of this exercise is to help students construct a mental model of pointers to pointers, much as C students do (in their different semantic model of what "pointer" means). Kirby
participants (2)
-
John Posner
-
kirby urner