SEESenv/web/html/ch6oop.html
changeset 28 514098969b11
parent 27 cb14131583c6
equal deleted inserted replaced
27:cb14131583c6 28:514098969b11
     1 <html>
     1 <html>
     2  <head>
     2 <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. oop</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head>
     3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     3 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch6oop">
     4   <title>
     4 <div class="titlepage"></div>
     5    Chapter. oop
     5 <div class="toc">
     6   </title>
     6 <p><b>Table of Contents</b></p>
     7   <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
     7 <dl><dt><span class="article"><a href="#id2745996">Classes and Objects</a></span></dt></dl>
     8   <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" />
     8 </div>
     9   <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" />
     9 <div class="article" title="Classes and Objects">
    10   <script type="text/javascript" src="/review/support/jquery-min.js">
    10 <div class="titlepage">
    11   </script>
    11 <div><div><h2 class="title">
    12   <script type="text/javascript" src="/review/support/form.js">
    12 <a name="id2745996"></a>Classes and Objects</h2></div></div>
    13   </script>
    13 <hr />
    14   <script type="text/javascript" src="/review/support/hsbook.js">
    14 </div>
    15   </script>
    15 <p id="ch6oop_1">In the previous sections we learnt about functions which provide certain level
    16  </head>
       
    17  <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
       
    18   <div class="chapter" id="ch6oop">
       
    19    <div class="titlepage">
       
    20    </div>
       
    21    <div class="toc">
       
    22     <p>
       
    23      <b>
       
    24       Table of Contents
       
    25      </b>
       
    26     </p>
       
    27     <dl>
       
    28      <dt>
       
    29       <span class="article">
       
    30        <a href="#id2471018">
       
    31         Classes and Objects
       
    32        </a>
       
    33       </span>
       
    34      </dt>
       
    35     </dl>
       
    36    </div>
       
    37    <div class="article" title="Classes and Objects">
       
    38     <div class="titlepage">
       
    39      <div>
       
    40       <div>
       
    41        <h2 class="title">
       
    42         <a name="id2471018">
       
    43         </a>
       
    44         Classes and Objects
       
    45        </h2>
       
    46       </div>
       
    47      </div>
       
    48      <hr />
       
    49     </div>
       
    50     <p id="ch6oop_1">
       
    51      In the previous sections we learnt about functions which provide certain level
       
    52 of abstraction to our code by holding the code which performs one or more
    16 of abstraction to our code by holding the code which performs one or more
    53 specific functionalities. We were able to use this function as many times as we
    17 specific functionalities. We were able to use this function as many times as we
    54 wanted. In addition to functions, Python also higher level of abstractions
    18 wanted. In addition to functions, Python also higher level of abstractions
    55 through
    19 through <span class="emphasis"><em>Classes</em></span> and <span class="emphasis"><em>Objects</em></span>. <span class="emphasis"><em>Objects</em></span> can be loosely defined as a
    56      <span class="emphasis">
       
    57       <em>
       
    58        Classes
       
    59       </em>
       
    60      </span>
       
    61      and
       
    62      <span class="emphasis">
       
    63       <em>
       
    64        Objects
       
    65       </em>
       
    66      </span>
       
    67      .
       
    68      <span class="emphasis">
       
    69       <em>
       
    70        Objects
       
    71       </em>
       
    72      </span>
       
    73      can be loosely defined as a
       
    74 collection of a set of data items and a set of methods. The data items can be
    20 collection of a set of data items and a set of methods. The data items can be
    75 any valid Python variable or any Python object. Functions enclosed within a class
    21 any valid Python variable or any Python object. Functions enclosed within a class
    76 are called as
    22 are called as <span class="emphasis"><em>methods</em></span>. If you are thinking if methods are functions why is there
    77      <span class="emphasis">
       
    78       <em>
       
    79        methods
       
    80       </em>
       
    81      </span>
       
    82      . If you are thinking if methods are functions why is there
       
    83 a distinction between the two? The answer to this will be given as we walk through
    23 a distinction between the two? The answer to this will be given as we walk through
    84 the concepts of
    24 the concepts of <span class="emphasis"><em>Classes</em></span> and <span class="emphasis"><em>Objects</em></span>. <span class="emphasis"><em>Classes</em></span> contain the definition for the
    85      <span class="emphasis">
    25 <span class="emphasis"><em>Objects</em></span>. <span class="emphasis"><em>Objects</em></span> are instances of <span class="emphasis"><em>Classes</em></span>.</p>
    86       <em>
    26 <p id="ch6oop_2">A class is defined using the keyword <span class="strong"><strong>class</strong></span> followed by the class name, in
    87        Classes
    27 turn followed by a semicolon. The statements that a <span class="emphasis"><em>Class</em></span> encloses are written
    88       </em>
    28 in a new block, i.e on the next indentation level:</p>
    89      </span>
    29 <pre class="programlisting">
    90      and
    30 class Employee:
    91      <span class="emphasis">
       
    92       <em>
       
    93        Objects
       
    94       </em>
       
    95      </span>
       
    96      .
       
    97      <span class="emphasis">
       
    98       <em>
       
    99        Classes
       
   100       </em>
       
   101      </span>
       
   102      contain the definition for the
       
   103      <span class="emphasis">
       
   104       <em>
       
   105        Objects
       
   106       </em>
       
   107      </span>
       
   108      .
       
   109      <span class="emphasis">
       
   110       <em>
       
   111        Objects
       
   112       </em>
       
   113      </span>
       
   114      are instances of
       
   115      <span class="emphasis">
       
   116       <em>
       
   117        Classes
       
   118       </em>
       
   119      </span>
       
   120      .
       
   121     </p>
       
   122     <p id="ch6oop_2">
       
   123      A class is defined using the keyword
       
   124      <span class="strong">
       
   125       <strong>
       
   126        class
       
   127       </strong>
       
   128      </span>
       
   129      followed by the class name, in
       
   130 turn followed by a semicolon. The statements that a
       
   131      <span class="emphasis">
       
   132       <em>
       
   133        Class
       
   134       </em>
       
   135      </span>
       
   136      encloses are written
       
   137 in a new block, i.e on the next indentation level:
       
   138     </p>
       
   139     <pre class="programlisting">
       
   140      class Employee:
       
   141   def setName(self, name):
    31   def setName(self, name):
   142     self.name = name
    32     self.name = name
   143 
    33 
   144   def getName(self):
    34   def getName(self):
   145     return self.name
    35     return self.name</pre>
   146     </pre>
    36 <p id="ch6oop_3">In the above example, we defined a class with the name Employee. We also defined
   147     <p id="ch6oop_3">
       
   148      In the above example, we defined a class with the name Employee. We also defined
       
   149 two methods, setName and getName for this class. It is important to note the
    37 two methods, setName and getName for this class. It is important to note the
   150 differences between the normal Python functions and class methods defined above.
    38 differences between the normal Python functions and class methods defined above.
   151 Each method of the class must take the same instance of the class(object) from
    39 Each method of the class must take the same instance of the class(object) from
   152 which it was called as the first argument. It is conventionally given the name,
    40 which it was called as the first argument. It is conventionally given the name,
   153      <span class="emphasis">
    41 <span class="emphasis"><em>self</em></span>. Note that <span class="emphasis"><em>self</em></span> is only a convention. You can use any other name, but
   154       <em>
       
   155        self
       
   156       </em>
       
   157      </span>
       
   158      . Note that
       
   159      <span class="emphasis">
       
   160       <em>
       
   161        self
       
   162       </em>
       
   163      </span>
       
   164      is only a convention. You can use any other name, but
       
   165 the first argument to the method will always be the same object of the class
    42 the first argument to the method will always be the same object of the class
   166 from which the method was called. The data memebers that belong to the class are
    43 from which the method was called. The data memebers that belong to the class are
   167 called as
    44 called as <span class="emphasis"><em>class attributes</em></span>. <span class="emphasis"><em>Class attributes</em></span> are preceded by the object of
   168      <span class="emphasis">
    45 the class and a dot. In the above example, <span class="emphasis"><em>name</em></span> is a class attribute since it
   169       <em>
    46 is preceded by the <span class="emphasis"><em>self</em></span> object. <span class="emphasis"><em>Class attributes</em></span> can be accessed from
   170        class attributes
    47 anywhere within the class.</p>
   171       </em>
    48 <p id="ch6oop_4">We can create objects of a class outside the class definition by using the same
   172      </span>
       
   173      .
       
   174      <span class="emphasis">
       
   175       <em>
       
   176        Class attributes
       
   177       </em>
       
   178      </span>
       
   179      are preceded by the object of
       
   180 the class and a dot. In the above example,
       
   181      <span class="emphasis">
       
   182       <em>
       
   183        name
       
   184       </em>
       
   185      </span>
       
   186      is a class attribute since it
       
   187 is preceded by the
       
   188      <span class="emphasis">
       
   189       <em>
       
   190        self
       
   191       </em>
       
   192      </span>
       
   193      object.
       
   194      <span class="emphasis">
       
   195       <em>
       
   196        Class attributes
       
   197       </em>
       
   198      </span>
       
   199      can be accessed from
       
   200 anywhere within the class.
       
   201     </p>
       
   202     <p id="ch6oop_4">
       
   203      We can create objects of a class outside the class definition by using the same
       
   204 syntax we use to call a function with no parameters. We can assign this object
    49 syntax we use to call a function with no parameters. We can assign this object
   205 to a variable:
    50 to a variable:</p>
   206     </p>
    51 <pre class="programlisting">
   207     <pre class="programlisting">
    52 emp = Employee()</pre>
   208      emp = Employee()
    53 <p id="ch6oop_5">In the above example, we create an object named <span class="emphasis"><em>emp</em></span> of the class <span class="emphasis"><em>Employee</em></span>.
   209     </pre>
       
   210     <p id="ch6oop_5">
       
   211      In the above example, we create an object named
       
   212      <span class="emphasis">
       
   213       <em>
       
   214        emp
       
   215       </em>
       
   216      </span>
       
   217      of the class
       
   218      <span class="emphasis">
       
   219       <em>
       
   220        Employee
       
   221       </em>
       
   222      </span>
       
   223      .
       
   224 All the attributes and methods of the class can be accessed by the object of the
    54 All the attributes and methods of the class can be accessed by the object of the
   225 class using the standard notation
    55 class using the standard notation <span class="emphasis"><em>object.attribute</em></span> or <span class="emphasis"><em>object.method()</em></span>.
   226      <span class="emphasis">
       
   227       <em>
       
   228        object.attribute
       
   229       </em>
       
   230      </span>
       
   231      or
       
   232      <span class="emphasis">
       
   233       <em>
       
   234        object.method()
       
   235       </em>
       
   236      </span>
       
   237      .
       
   238 Although the first parameter of a class method is the self object, it must not
    56 Although the first parameter of a class method is the self object, it must not
   239 be passed as an argument when calling the method. The
    57 be passed as an argument when calling the method. The <span class="emphasis"><em>self</em></span> object is implicitly
   240      <span class="emphasis">
       
   241       <em>
       
   242        self
       
   243       </em>
       
   244      </span>
       
   245      object is implicitly
       
   246 passed to the method by the Python interpreter. All other arguments passing rules
    58 passed to the method by the Python interpreter. All other arguments passing rules
   247 like default arguments, keyword arguments, argument packing and unpacking follow
    59 like default arguments, keyword arguments, argument packing and unpacking follow
   248 the same rules as those for ordinary Python functions:
    60 the same rules as those for ordinary Python functions:</p>
   249     </p>
    61 <pre class="programlisting">
   250     <pre class="programlisting">
    62 &gt;&gt;&gt; emp.setName('John')
   251      &gt;&gt;&gt; emp.setName('John')
       
   252 &gt;&gt;&gt; name = emp.getName()
    63 &gt;&gt;&gt; name = emp.getName()
   253 &gt;&gt;&gt; print name
    64 &gt;&gt;&gt; print name
   254 John
    65 John
   255 &gt;&gt;&gt; print emp.name
    66 &gt;&gt;&gt; print emp.name
   256 John
    67 John</pre>
   257     </pre>
    68 <p id="ch6oop_6">If we at all try to access a class attribute before assigning a value to it, i.e
   258     <p id="ch6oop_6">
       
   259      If we at all try to access a class attribute before assigning a value to it, i.e
       
   260 before creating it, Python raises the same error as it would raise for the
    69 before creating it, Python raises the same error as it would raise for the
   261 accessing undefined variable:
    70 accessing undefined variable:</p>
   262     </p>
    71 <pre class="programlisting">
   263     <pre class="programlisting">
    72 &gt;&gt;&gt; emp = Employee()
   264      &gt;&gt;&gt; emp = Employee()
       
   265 &gt;&gt;&gt; emp.name
    73 &gt;&gt;&gt; emp.name
   266 Traceback (most recent call last):
    74 Traceback (most recent call last):
   267   File "class.py", line 10, in &lt;module&gt;
    75   File "class.py", line 10, in &lt;module&gt;
   268     print e.name
    76     print e.name
   269 AttributeError: Employee instance has no attribute 'name'
    77 AttributeError: Employee instance has no attribute 'name'</pre>
   270     </pre>
    78 </div>
   271    </div>
    79 </div></body>
   272   </div>
       
   273  </body>
       
   274 </html>
    80 </html>
       
    81