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

SVN User <[email protected]> Sat, 15 May 2004 01:44:20 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-05-15 01:44:17 -0400 (Sat, 15 May 2004)
New Revision: 490

Added:
   trunk/docs/tutorial/tutorial6.htm
   trunk/pr/tutorial/tut8.pr
   trunk/pr/tutorial/tut9.pr
Modified:
   trunk/docs/tutorial/index.htm
   trunk/docs/tutorial/tutorial3.htm
   trunk/docs/tutorial/tutorial5.htm
Log:
added attributes, vars, and functions to tutorial

Modified: trunk/docs/tutorial/index.htm
===================================================================
--- trunk/docs/tutorial/index.htm	2004-05-14 22:36:02 UTC (rev 489)
+++ trunk/docs/tutorial/index.htm	2004-05-15 05:44:17 UTC (rev 490)
@@ -118,7 +118,7 @@
                   <li><a href="tutorial4.htm#keyrules">Rules about Keys</a> 
                   <li><a href="tutorial4.htm#dictenter">Entering a Dictionary</a> 
                   <li><a href="tutorial4.htm#dictmutable">Assignment and del</a> 
-                  <li><a href="tutorial4.htm#mutlist">Dictionary Functions and 
+                  <li><a href="tutorial4.htm#dictfunc">Dictionary Functions and 
                     !</a> 
                 </ul>
             </ul>
@@ -147,10 +147,46 @@
                   <li><a href="tutorial5.htm#break">Break Statement</a> 
                   <li><a href="tutorial5.htm#continue">Continue Statement</a> 
                   <li><a href="tutorial5.htm#tags">Loop Block Tags</a> 
-                  <li><a href="tutorial5.htm#else">Else Clause</a>
+                  <li><a href="tutorial5.htm#else">Else Clause</a> 
                   <li><a href="tutorial5.htm#bcaesmpl">Sample Code</a> 
                 </ul>
             </ul>
+          <li>5. <a href="tutorial6.htm#oav">Attributes, Variables, &amp; Functions 
+            </a> 
+            <ul>
+              <li><a href="tutorial6.htm#attr">5.1 Attributes</a> 
+                <ul>
+                  <li><a href="tutorial6.htm#attrkey">Attribute Keys</a> 
+                  <li><a href="tutorial6.htm#symbol">Symbols</a> 
+                  <li><a href="tutorial6.htm#period">Period Operator</a>
+                  <li><a href="tutorial6.htm#attrass">Attribute Assignment &amp; 
+                    Del </a> 
+                </ul>
+              <li><a href="tutorial6.htm#vsw">5.2 Variables, Scope, &amp; With 
+                </a> 
+                <ul>
+                  <li><a href="tutorial6.htm#main">Main Module</a>
+                  <li><a href="tutorial6.htm#attr">Variable Scope</a> 
+                  <li><a href="tutorial6.htm#with">With Statement</a>
+                </ul>
+              <li><a href="tutorial6.htm#funcdef">5.3 Functions &amp; Def Statement</a> 
+                <ul>
+                  <li><a href="tutorial6.htm#func">Functions</a> 
+                  <li><a href="tutorial6.htm#def">Def Statement</a> 
+                  <li><a href="tutorial6.htm#funcform">Function Calls</a> 
+                  <li><a href="tutorial6.htm#pos">Positional Params</a> 
+                  <li><a href="tutorial6.htm#defval">Default Values</a> 
+                  <li><a href="tutorial6.htm#namematch">Keyword Params</a> 
+                  <li><a href="tutorial6.htm#starvar">Variable Parameters</a>
+                  <li><a href="tutorial6.htm#actstarvar">Variable Arguments</a> 
+                  <li><a href="tutorial6.htm#sampleparms">Sample Parameter Passing</a> 
+                  <li><a href="tutorial6.htm#localscope">Local Scope</a> 
+                  <li><a href="tutorial6.htm#passref">Pass By Reference</a> 
+                  <li><a href="tutorial6.htm#noneobj">None Object</a> 
+                  <li><a href="tutorial6.htm#rtrnstmt">Return Statement</a>
+                </ul>
+              
+            </ul>
         </ul>
       </td>
     </tr>
Modified: trunk/docs/tutorial/tutorial3.htm
===================================================================
--- trunk/docs/tutorial/tutorial3.htm	2004-05-14 22:36:02 UTC (rev 489)
+++ trunk/docs/tutorial/tutorial3.htm	2004-05-15 05:44:17 UTC (rev 490)
@@ -21,7 +21,7 @@
     <tr> 
       <td width="114" height="25" valign="top">&nbsp;<a href="http://prothon.org">Prothon 
         Home</a></td>
-      <td width="194" valign="middle" align="center" height="25"><a href="tutorial1.htm">Previous 
+      <td width="194" valign="middle" align="center" height="25"><a href="tutorial2.htm">Previous 
         Page</a></td>
       <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial 
         Outline</a></td>
@@ -227,7 +227,7 @@
     <tr> 
       <td width="114" height="25" valign="top">&nbsp;<a href="http://prothon.org">Prothon 
         Home</a></td>
-      <td width="194" valign="middle" align="center" height="25"><a href="tutorial1.htm">Previous 
+      <td width="194" valign="middle" align="center" height="25"><a href="tutorial2.htm">Previous 
         Page</a></td>
       <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial 
         Outline</a></td>
Modified: trunk/docs/tutorial/tutorial5.htm
===================================================================
--- trunk/docs/tutorial/tutorial5.htm	2004-05-14 22:36:02 UTC (rev 489)
+++ trunk/docs/tutorial/tutorial5.htm	2004-05-15 05:44:17 UTC (rev 490)
@@ -21,11 +21,11 @@
     <tr> 
       <td width="114" height="25" valign="top">&nbsp;<a href="http://prothon.org">Prothon 
         Home</a></td>
-      <td width="194" valign="middle" align="center" height="25"><a href="tutorial3.htm">Previous 
+      <td width="194" valign="middle" align="center" height="25"><a href="tutorial4.htm">Previous 
         Page</a></td>
       <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial 
         Outline</a></td>
-      <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next 
+      <td width="139" valign="middle" align="center" height="25"><a href="tutorial6.htm">Next 
         Page</a></td>
     </tr>
   </table>
@@ -276,11 +276,11 @@
     <tr> 
       <td width="114" height="25" valign="top">&nbsp;<a href="http://prothon.org">Prothon 
         Home</a></td>
-      <td width="194" valign="middle" align="center" height="25"><a href="tutorial3.htm">Previous 
+      <td width="194" valign="middle" align="center" height="25"><a href="tutorial4.htm">Previous 
         Page</a></td>
       <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial 
         Outline</a></td>
-      <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next 
+      <td width="139" valign="middle" align="center" height="25"><a href="tutorial6.htm">Next 
         Page</a></td>
     </tr>
   </table>
Added: trunk/docs/tutorial/tutorial6.htm
===================================================================
--- trunk/docs/tutorial/tutorial6.htm	2004-05-14 22:36:02 UTC (rev 489)
+++ trunk/docs/tutorial/tutorial6.htm	2004-05-15 05:44:17 UTC (rev 490)
@@ -0,0 +1,275 @@
+<html>
+<head>
+<title>Prothon Tutorial - 5.0</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+</head>
+
+<body bgcolor="#FFFFFF" text="#000000">
+<div align="center">
+  <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFCC">
+    <tr>
+      <td height="121" width="109"><img src="../images/logo_left.jpg" width="109" height="135"></td>
+      <td valign="center" height="43"> 
+        <div align="center"><font size="5" face="Verdana, Arial, Helvetica, sans-serif">Prothon 
+          Tutorial</font></div>
+      </td>
+      <td valign="center" height="121" width="106"><img src="../images/logo_right.jpg" width="109" height="135"></td>
+    </tr>
+  </table>
+  <br>
+  <table width="630" border="0" cellpadding="0" cellspacing="0">
+    <tr> 
+      <td width="114" height="25" valign="top">&nbsp;<a href="http://prothon.org">Prothon 
+        Home</a></td>
+      <td width="194" valign="middle" align="center" height="25"><a href="tutorial5.htm">Previous 
+        Page</a></td>
+      <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial 
+        Outline</a></td>
+      <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next 
+        Page</a></td>
+    </tr>
+  </table>
+  <br>
+  <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
+    <tr> 
+      <td height="3515" valign="top"> 
+        <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oav"></a>5.0 
+          Attributes, Variables, &amp; Functions</font></p>
+        <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="attr"></a>5.1 
+          Attributes</font></p>
+        <p>It's time to say it again. &quot;Everything in Prothon is an object&quot;. 
+          So what exactly is an object? We know objects hold binary data like 
+          integers and strings. Creating objects that hold binary data can be 
+          done only by C code modules and we won't cover those in this tutorial. 
+        </p>
+        <p>Objects can also hold other objects of any type. I'm not talking about 
+          containers like lists and dictionaries. Those are specialized objects 
+          just for containment. I'm talking about all objects. Every object, whether 
+          it be an integer, string, or even a vanilla object with no type, can 
+          hold other objects called attributes.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="attrkey"></a></font>So 
+          objects are things that can hold binary data and attributes. Attributes 
+          are stored in an object much like dictionaries hold objects. They are 
+          stored with a key, but the attribute's keys can't be just any old immutable 
+          object as in a dictionary. In an attribute the key must be a legal Prothon 
+          symbol, like &quot;x&quot;, or &quot;hasKey_?&quot;.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="symbol"></a></font>A 
+          Prothon symbol can be any combination of the digits 0 to 9, the letters 
+          a to z, A to Z, and the underbar ( _ ). It cannot start with a digit. 
+          It may also optionally have a single exclamation mark ( ! ) or question 
+          mark ( ? ) at the end.</p>
+        <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>
+        <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>
+        <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 
+          you run a Prothon source file, there is a special object called a module 
+          created. We will learn more about modules later. This module, called 
+          &quot;Main1&quot;, has attributes as all objects are capable of. It's 
+          attributes are special in that they are the &quot;variables&quot; of 
+          the code that runs at the main level of the code. So if the first line 
+          of a source file says &quot;x=1&quot;, you are really saying &quot;Main1.x 
+          = 1&quot;.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="attr"></a></font>Every 
+          variable in all Prothon code is an attribute of some object. So variables 
+          and attributes are the same thing. When you use variables though, quite 
+          often the object that the variable is an attribute of is hidden and 
+          it sometimes takes special knowledge of the rules to figure out what 
+          that object is. This object that holds variables is called a &quot;scope 
+          object&quot; because it holds the variables for a certain scope of the 
+          code. A scope is something like the main level, the inside of a function, 
+          etc</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="with"></a></font>Prothon 
+          has a special &quot;with&quot; statement just to set the current scope 
+          object. This is convenient for when you are about to work on one object 
+          extensively and want to save typing and to concentrate on that object. 
+          Later you will see something &quot;with&quot; is very useful for. Here 
+          is an example for now:</p>
+        <pre>>>> x = [1]
+[1]
+>>> with x:
+...    a=1
+...
+>>> print x.a
+1
+>>></pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="funcdef"></a>5.3 
+          Functions &amp; the Def Statement</font></p>
+        <p>A function allows you to factor out code to reuse in multiple places 
+          and to abstract a function into a higher-level concept to call.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="func"></a></font>Prothon 
+          is a dynamic language, which means you define the functions at run-time, 
+          not during compile time in advance. Once again, &quot;everything is 
+          an object&quot; so functions are objects also. You create a function 
+          object by using the &quot;def statement. A function definition consists 
+          of the code to run, the parameters to pass in when the function starts 
+          and the value to return when the function ends. </p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="def"></a></font>The 
+          def statement form is &quot;def name(params): body&quot;. The name is 
+          the variable that will hold the new function object being created, just 
+          as if the name was on the left side of an assignment statement (name 
+          = function). The body is the body of code that is executed just as in 
+          the &quot;if&quot;, &quot;for&quot;, and other statements. Params is 
+          the list of variable names that the incoming parameters will be assigned 
+          to when the function is first started. These are called the formal parameters 
+          as opposed to the actual parameters that are in the function call. </p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="funcform"></a></font>Function 
+          calls look similar to the definition of a function. They have the form 
+          &quot;name(params)&quot; where name is the variable holding the function 
+          object and params are the actual parameters to be passed to the formal 
+          parameters in the function definition. Let's look at some examples:</p>
+        <pre># Prothon source file tut8.pr
+
+def func1(x):
+    print "x is",x
+print func1     # func1 is an object that hasn't run
+func1(99)       # now it runs</pre>
+        <p>This is the output of the program: </p>
+        <pre>&lt;func:93c120&gt; 
+x is 99</pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="pos"></a></font>Prothon 
+          has some very sophisticated methods for matching the actual parameters 
+          to the formal parameters. The simplest method is positional matching. 
+          If func is defined as func(x,y,z) and is called as func(1,2,3) then 
+          x=1, y=2, and z=3 after the function starts. </p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="defval"></a></font>The 
+          next param passing trick is the use of default values, also known as 
+          optional params. If the formal param in the function definition has 
+          the form &quot;name = value&quot;, then that param can be missing in 
+          the actual params and the default value will be used instead. If the 
+          func is defined as func(x, y=11, z=12) and it is called as func(1,2) 
+          then x=1, y=2, and z=12 inside the function. Note that the formal params 
+          with defaults must follow the params without defaults to avoid ambiguities.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="namematch"></a></font>The 
+          third param technique is to match the formal and actual params by keyword 
+          instead of by position. To do this, you use the form &quot;name = value&quot; 
+          in the actual param in the function call. This allows you to place the 
+          actual params in any order and it makes the call self-documenting. If 
+          the func is defined as func(x,y,z) and it is called as func(y=1, z=2, 
+          x=3), then x=3, y=1, and z=2 inside the function.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="starvar"></a></font>Finally, 
+          there is a very powerful technique that allows a variable number of 
+          parameters to be passed to or accepted into a function. If a formal 
+          parameter list ends in &quot;*var&quot; then all actual positional parameters 
+          &quot;left over&quot; that haven't been assigned to formal positional 
+          parameters are put into a list named var. Likewise, if the formal list 
+          ends with a &quot;**var&quot; then all the given actual named parameters 
+          that don't match any parameters in the formal list are put into a dictionary 
+          named var with the name as the key and the actual value as the value. 
+          If you put both &quot;*var1, **var2&quot; into the formal list then 
+          all paramaters are guaranteed to be captured. </p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="actstarvar"></a></font>In 
+          the other direction, you can pass *var1 and **var2 as actual parameters 
+          in a call where var is a list of actual positional parameters and var2 
+          is a dictionary of named actual parameters. This allows you to calculate 
+          an arbitrary number of parameters to pass to a function. </p>
+        <p>Note that combining these two directions allows you to accept every 
+          possible set of parameters in a function by using the definition func1(*args, 
+          **kwargs) and then passing on that arbitray set of parameters to another 
+          function with the call func2(*args, **kwargs).</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="sampleparms"></a></font>Here 
+          are some samples of parameter passing:</p>
+        <pre># Prothon source file tut9.pr
+
+def func(x,y=1,*args,**kwargs):
+    print 'x:',x,'y:',y
+    for arg in args:
+        print 'arg:',arg
+    for kwarg in kwargs.items():
+        print 'kwarg:', kwarg[0], kwarg[1]
+    print
+
+func(0)
+func(1,2)
+func(1,x=11,y=22,z=33)
+func(1,2,3,4)
+func(1,2,3,4,a=11,b=22,c=33)</pre>
+        <p>Here is the output:</p>
+        <pre>x: 0 y: 1 
+
+x: 1 y: 2 
+
+x: 1 y: 22 
+kwarg: x 11 
+kwarg: z 33 
+
+x: 1 y: 2 
+arg: 3 
+arg: 4 
+
+x: 1 y: 2 
+arg: 3 
+arg: 4 
+kwarg: a 11 
+kwarg: b 22 
+kwarg: c 33</pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="localscope"></a></font>So 
+          what happens to the parameters when they are passed in to the function? 
+          When the function starts, a new scope object is created from scratch 
+          called the &quot;local scope&quot;. This is where the local variables 
+          of the function are stored. After the local scope object is created, 
+          the actual parameters are copied into that scope object just as if an 
+          assignment statement happened for each parameter. </p>
+        <p>Let's say that the scope object is called &quot;local&quot; (it's not 
+          really, it doesn't have a name). Then a function defined as func(a, 
+          b, c) and called as func(1, 'a', obj) would cause parameter passing 
+          that is equivalent to this code:</p>
+        <pre>local.a = 1
+local.b = 'a'
+local.c = obj</pre>
+        <p>Then inside the function, any reference to a, b or c will default to 
+          the &quot;local scope&quot; and actually use local.a, local.b and local.c. 
+        </p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="passref"></a></font>Notice 
+          that the last parameter passed was an obj that might be mutable. Just 
+          as in an assignment, the thing passed is just a reference to the obj, 
+          not a copy of the object. This means that the variable obj outside of 
+          the function is the same exact object as the c object inside the function. 
+          If you make a change to c then obj will change also. This cannot happen 
+          with a or b since numbers and strings are immutable and cannot be changed.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="noneobj"></a></font>Function 
+          always return a value to the caller. In the functions we have been looking 
+          at they have been returning the default value of None. None is a special 
+          unique object like the True and False objects. Speaking of True and 
+          False, None.bool_() returns False so None is False in expressions and 
+          &quot;if&quot; and &quot;while&quot; statements.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="rtrnstmt"></a></font>To 
+          return something other than None, or to stop executing a function somewhere 
+          other than at the end, you use the &quot;return&quot; statement. This 
+          statement is somewhat like &quot;break&quot; in that it stops execution, 
+          but &quot;return&quot; returns from the function block of code no matter 
+          how deeply nested. If return has a parameter, that is returned as the 
+          return value of the function, else the None object is returned.</p>
+        <pre>>>> def squared(x):
+...    return x*x
+...
+>>> squared(3)
+9
+>>></pre>
+      </td>
+    </tr>
+  </table>
+  <table width="630" border="0" cellpadding="0" cellspacing="0">
+    <tr> 
+      <td width="114" height="25" valign="top">&nbsp;<a href="http://prothon.org">Prothon 
+        Home</a></td>
+      <td width="194" valign="middle" align="center" height="25"><a href="tutorial5.htm">Previous 
+        Page</a></td>
+      <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial 
+        Outline</a></td>
+      <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next 
+        Page</a></td>
+    </tr>
+  </table>
+</div>
+</body>
+</html>
Added: trunk/pr/tutorial/tut8.pr
===================================================================
--- trunk/pr/tutorial/tut8.pr	2004-05-14 22:36:02 UTC (rev 489)
+++ trunk/pr/tutorial/tut8.pr	2004-05-15 05:44:17 UTC (rev 490)
@@ -0,0 +1,8 @@
+
+# Prothon source file tut8.pr
+
+def func1(x):
+    print "x is",x
+print func1     # func1 is an object that hasn't run
+func1(99)       # now it runs
+
Added: trunk/pr/tutorial/tut9.pr
===================================================================
--- trunk/pr/tutorial/tut9.pr	2004-05-14 22:36:02 UTC (rev 489)
+++ trunk/pr/tutorial/tut9.pr	2004-05-15 05:44:17 UTC (rev 490)
@@ -0,0 +1,17 @@
+
+# Prothon source file tut9.pr
+
+def func(x,y=1,*args,**kwargs):
+    print 'x:',x,'y:',y
+    for arg in args:
+        print 'arg:',arg
+    for kwarg in kwargs.items():
+        print 'kwarg:', kwarg[0], kwarg[1]
+    print
+
+func(0)
+func(1,2)
+func(1,x=11,y=22,z=33)
+func(1,2,3,4)
+func(1,2,3,4,a=11,b=22,c=33)
+