rev 505 - in trunk: docs/tutorial pr/tutorial

SVN User <[email protected]> Tue, 18 May 2004 20:30:36 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-05-18 20:30:33 -0400 (Tue, 18 May 2004)
New Revision: 505

Modified:
   trunk/docs/tutorial/outline2.htm
   trunk/docs/tutorial/tutorial10.htm
   trunk/docs/tutorial/tutorial3.htm
   trunk/docs/tutorial/tutorial6.htm
   trunk/pr/tutorial/tut14.pr
Log:
added caller keyword to tutorial

Modified: trunk/docs/tutorial/outline2.htm
===================================================================
--- trunk/docs/tutorial/outline2.htm	2004-05-18 23:18:58 UTC (rev 504)
+++ trunk/docs/tutorial/outline2.htm	2004-05-19 00:30:33 UTC (rev 505)
@@ -90,29 +90,34 @@
                   <li><a href="tutorial9.htm#miex">Example</a> 
                 </ul>
             </ul>
-          <li>7. <a href="tutorial10.htm#vsm">Variable Scope &amp; Module Structure</a> 
+          <li>7. <a href="tutorial10.htm#vsm">Local Scopes &amp; Module Structure</a> 
             <ul>
               <li><a href="tutorial10.htm#lvasc">7.1 Local Var Access &amp; Scope 
                 Chain </a> 
                 <ul>
-                  <li><a href="tutorial10.htm#locscobj">Local Scope Object</a>
-                  <li><a href="tutorial10.htm#lschain">Local Scope Chain</a>
+                  <li><a href="tutorial10.htm#locscobj">Local Scope Object</a> 
+                  <li><a href="tutorial10.htm#lschain">Local Scope Chain</a> 
                   <li><a href="tutorial10.htm#blcks">Blocks Create Scopes</a> 
                   <li><a href="tutorial10.htm#noglobal">No Python Global</a> 
-                  <li><a href="tutorial10.htm#object">Chain Ends at Object</a>
+                  <li><a href="tutorial10.htm#object">Chain Ends at Object</a> 
                 </ul>
-              
               <li><a href="tutorial10.htm#slvok">7.2 Setting Local Vars &amp; 
                 Outer Keyword</a> 
                 <ul>
+                  <li><a href="tutorial10.htm#shadow">Shadowing Variables</a> 
                   <li><a href="tutorial10.htm#lvasc">Assignment Sets Scope</a> 
                   <li><a href="tutorial10.htm#outer">Outer Keyword</a> 
-                  <li><a href="tutorial10.htm#closure">Closures</a>
+                  <li><a href="tutorial10.htm#closure">Closures</a> 
                 </ul>
-              
+              <li><a href="tutorial10.htm#callkywd">7.3 Caller Keyword</a> 
+                <ul>
+                  <li><a href="tutorial10.htm#caller">Caller Explained</a> 
+                  <li><a href="tutorial10.htm#calleuses">Uses for Caller &amp; 
+                    Example</a>
+                </ul>
             </ul>
         </ul>
-        </td>
+      </td>
     </tr>
   </table>
 
Modified: trunk/docs/tutorial/tutorial10.htm
===================================================================
--- trunk/docs/tutorial/tutorial10.htm	2004-05-18 23:18:58 UTC (rev 504)
+++ trunk/docs/tutorial/tutorial10.htm	2004-05-19 00:30:33 UTC (rev 505)
@@ -34,7 +34,7 @@
     <tr> 
       <td height="3165" valign="top"> 
         <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="vsm"></a>7.0 
-          Variable Scopes &amp; Module Structure</font></p>
+          Local Scopes &amp; Module Structure</font></p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="lvasc"></a>7.1 
           Local Var Access &amp; Scope Chain</font></p>
         <p>As we learned is section 5, all variables are attributes of scope objects. 
@@ -88,28 +88,36 @@
           &quot;global&quot; is just the last scope in the &quot;local scope chain&quot; 
           in Prothon.</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="object"></a></font>What 
-          happens when it isn't found in scope-1? In that case the search continues 
-          in the prototype chain of scope-1, which eventually ends up at Object, 
-          just like the &quot;self&quot; search. This means that all searches 
-          end up at Object. This is important because built-in functions and methods 
-          like Len(str) and Cmp(a,b) are stored in Object.</p>
+          happens when an attribute isn't found in scope-1? In that case the search 
+          continues in the prototype chain of scope-1, which eventually ends up 
+          at Object, just like the &quot;self&quot; search. This means that all 
+          searches end up at Object. This is important because built-in functions 
+          and methods like Len(str) and Cmp(a,b) are stored in Object.</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="slvok"></a>7.2 
           Setting Local Vars &amp; Outer Keyword</font></p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="shadow"></a></font>What 
+          if you wanted to access the variable named &quot;a&quot; in scope-1, 
+          but there was already a local variable named &quot;a&quot; in the immediate 
+          &quot;local&quot; scope object? This problem of a name in a closer scope 
+          blocking access to a more remote scope if called &quot;shadowing&quot;. 
+          We will show a solution to this in just a moment.</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="lvasc"></a></font>The 
           last section concerned read access only. In other words access on the 
           right side of assignment statements. If you noticed, in the tut13 example, 
           each of the variable's (a, b, or c) scope locations was determined by 
           where it was set on the left side on an assignment statement. If you 
-          wanted to change the value of scop-1's variable &quot;a&quot; from inside 
-          of func(), and you just used the statement &quot;a = 9&quot;, you would 
-          actually create a new local variable &quot;a&quot; inside of scope-3 
-          in func(), not change &quot;a&quot; in scope-1.</p>
+          wanted to change the value of scope-1's variable &quot;a&quot; from 
+          inside of func(), and you just used the statement &quot;a = 9&quot;, 
+          you would actually create a new variable &quot;a&quot; inside of scope-3 
+          in func(), not change the value of &quot;a&quot; in scope-1. How do 
+          you assign a value to the scope-1 variable &quot;a&quot;?</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="outer"></a></font>In 
-          order to solve this problem, Prothon has a special keyword named &quot;outer&quot; 
-          that when prefixed to a variable name, tells the interpreter to skip 
-          over the first local scope when looking for that attribute and also 
-          to only use existing attributes when assigning values, never create 
-          new attributes. Here is an example of it's usage:</p>
+          order to solve both the &quot;shadowing&quot; problem and the assignment 
+          problem, Prothon has a special keyword named &quot;outer&quot; that 
+          when prefixed to a variable name, tells the interpreter to skip over 
+          the first local scope when looking for that attribute, and also to only 
+          use existing attributes when assigning values, never create new attributes. 
+          Here is an example of it's usage:</p>
         <pre>
 # Prothon source file tut14.pr
  
@@ -119,9 +127,11 @@
     b = 2    # This is second scope-2 inside scope-1
     
     def func():
-        a = 11
+        c = 33    # this is third scope-3 inside scope-2
+
+
+        a = 11    # these shadow the outer a and b variables
         b = 22
-        c = 33    # this is third scope-3 inside scope-2
         print a, b, c       
         
         print outer.a, outer.b    # accessing outer variables
@@ -159,6 +169,34 @@
 func()    # prints 2
 func()    # prints 3
 func()    # prints 4</pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="callkywd"></a>7.3 
+          Caller Keyword</font></p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="caller"></a></font>There 
+          is one last keyword that relates to &quot;local&quot; scope objects. 
+          This one is simple though. The &quot;caller&quot; keyword is a prefix 
+          that, like &quot;self&quot;, is only available in functions. It gives 
+          you access to the &quot;local&quot; scope object of the code that called 
+          the currently running function. There is no scope chain involved, so 
+          like &quot;self&quot;, if an attribute lookup fails it uses the prototype 
+          chain for lookup. </p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="calleuses"></a></font>There 
+          is some debate about the usefulness of &quot;caller&quot;. It provides 
+          a simple macro-like access to variables in the calling code. It also 
+          provides a (not very good) alternative to parameter passing. </p>
+        <pre>>>> def func():
+...    print caller.a, caller.b
+...    caller.a = 11
+...    print caller.a, caller.b
+...
+>>> a = 1
+1
+>>> b = 2
+2
+>>> func()
+1 2
+11 2
+>>></pre>
+        <p>&nbsp;</p>
       </td>
     </tr>
   </table>
Modified: trunk/docs/tutorial/tutorial3.htm
===================================================================
--- trunk/docs/tutorial/tutorial3.htm	2004-05-18 23:18:58 UTC (rev 504)
+++ trunk/docs/tutorial/tutorial3.htm	2004-05-19 00:30:33 UTC (rev 505)
@@ -65,12 +65,12 @@
 >>> "hello " + 33
 hello 33
 >>></pre>
-        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="varass"></a></font>Variables 
-          are labeled containers, such as &quot;x&quot;, that hold objects. You 
-          put an object into a variable using an assignment statement such as 
-          &quot;x = 3&quot;. Then later you can use the variable in the place 
-          of the constant object 3. Note that you can have multiple items assigned 
-          at once, which is very useful for swapping items:</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="varass"></a></font>Prothon 
+          variables are all stored references to objects. You put an object reference 
+          into a variable using an assignment statement such as &quot;x = 3&quot;. 
+          Then later you can use the variable in the place of the constant object 
+          3. Note that you can have multiple items assigned at once, which is 
+          very useful for swapping items:</p>
         <pre>>> x, y = 1, 2
 1
 >>> print x, y
@@ -109,7 +109,7 @@
           will work in Prothon with one exception. That exception is that the 
           number may not end in a period. When you type a number that ends in 
           the decimal point add a zero, I.E. change &quot;1.&quot; to &quot;1.0&quot;. 
-        </p>
+          See <a href="tutorial7.htm#numdp">here</a> for the reason.</p>
         <p>Note that unlike many languages, when you divide two integers, you 
           get a floating point number (unless you use a special // symbol which 
           will be explained later).</p>
Modified: trunk/docs/tutorial/tutorial6.htm
===================================================================
--- trunk/docs/tutorial/tutorial6.htm	2004-05-18 23:18:58 UTC (rev 504)
+++ trunk/docs/tutorial/tutorial6.htm	2004-05-19 00:30:33 UTC (rev 505)
@@ -61,14 +61,15 @@
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="period"></a></font>If 
           you have an object stored in a variable, then you can access the attributes 
           of that object in a simple way using the period operator. If there is 
-          an object in x and there is an object that is an attribute of x with 
-          the key &quot;y&quot;, then you can just say &quot;y is an attribute 
-          of &quot;x&quot; and you can access it with &quot;x.y&quot;.</p>
+          an object in &quot;x&quot; and there is an object that is an attribute 
+          of &quot;x&quot; with the key &quot;y&quot;, then you can just say &quot;y&quot; 
+          is an attribute of &quot;x&quot; and you can access it with &quot;x.y&quot;.</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="attrass"></a></font>Just 
           like dictionaries, you can add new attributes or replace an existing 
-          one by simply using using an assignment statement. x.y = 1 will create 
-          a new attribute y or replace an old one. Also, you can delete an attribute 
-          with the &quot;del&quot; delete command just like dictionaries.</p>
+          one by simply using using an assignment statement. &quot;x.y = 1&quot; 
+          will create a new attribute &quot;y&quot; or replace an old one. Also, 
+          you can delete an attribute with the &quot;del&quot; delete command 
+          just like dictionaries.</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="vsw"></a>5.2 
           Variables, Scope, &amp; With</font></p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="main"></a></font>When 
Modified: trunk/pr/tutorial/tut14.pr
===================================================================
--- trunk/pr/tutorial/tut14.pr	2004-05-18 23:18:58 UTC (rev 504)
+++ trunk/pr/tutorial/tut14.pr	2004-05-19 00:30:33 UTC (rev 505)
@@ -7,9 +7,10 @@
     b = 2    # This is second scope-2 inside scope-1
     
     def func():
-        a = 11
+        c = 33    # this is third scope-3 inside scope-2
+
+        a = 11    # these shadow the outer a and b variables
         b = 22
-        c = 33    # this is third scope-3 inside scope-2
         print a, b, c       
         
         print outer.a, outer.b    # accessing outer variables