web/html/ch02-oop.html~
changeset 1 672eaaab9204
parent 0 8083d21c0020
child 2 52d12eb31c30
equal deleted inserted replaced
0:8083d21c0020 1:672eaaab9204
     1 <html>
       
     2 <head>
       
     3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
       
     4 <title>Classes and Objects</title>
       
     5 <link rel="stylesheet" href="/review/support/styles.css" type="text/css">
       
     6 <meta name="generator" content="DocBook XSL Stylesheets V1.74.3">
       
     7 <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png">
       
     8 <script type="text/javascript" src="/review/support/jquery-min.js"></script>
       
     9 <script type="text/javascript" src="/review/support/form.js"></script>
       
    10 <script type="text/javascript" src="/review/support/hsbook.js"></script>
       
    11 </head>
       
    12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
       
    13 <div class="chapter" lang="en" id="chap02_basicoop">
       
    14 <div class="article" title="Classes and Objects">
       
    15 <div class="titlepage">
       
    16 <div><div><h2 class="title">
       
    17 <a name="id2580083"></a>Classes and Objects</h2></div></div>
       
    18 <hr>
       
    19 </div>
       
    20 <p><a name="x_c2"></a>In the previous sections we learnt about functions which provide certain level of abstraction to our code by holding the code which performs one or more specific functionalities. We were able to use this function as many times as we wanted. In addition to functions, Python also higher level of abstractions through 
       
    21     <span class="emphasis"><em>Classes</em></span> and 
       
    22     <span class="emphasis"><em>Objects</em></span>. 
       
    23     <span class="emphasis"><em>Objects</em></span> can be loosely defined as a collection of a set of data items and a set of methods. The data items can be any valid Python variable or any Python object. Functions enclosed within a class are called as 
       
    24     <span class="emphasis"><em>methods</em></span>. If you are thinking if methods are functions why is there a distinction between the two? The answer to this will be given as we walk through the concepts of 
       
    25     <span class="emphasis"><em>Classes</em></span> and 
       
    26     <span class="emphasis"><em>Objects</em></span>. 
       
    27     <span class="emphasis"><em>Classes</em></span> contain the definition for the 
       
    28     <span class="emphasis"><em>Objects</em></span>. 
       
    29     <span class="emphasis"><em>Objects</em></span> are instances of 
       
    30     <span class="emphasis"><em>Classes</em></span>.
       
    31   </p>
       
    32 <p><a name="x_c3"></a>A class is defined using the keyword 
       
    33     <span class="strong"><strong>class</strong></span> followed by the class name, in turn followed by a semicolon. The statements that a 
       
    34     <span class="emphasis"><em>Class</em></span> encloses are written in a new block, i.e on the next indentation level:
       
    35   </p>
       
    36 <pre class="programlisting">class Employee:
       
    37   def setName(self, name):
       
    38     self.name = name
       
    39 
       
    40   def getName(self):
       
    41     return self.name
       
    42 
       
    43 </pre>
       
    44 <p><a name="x_c4"></a>In the above example, we defined a class with the name Employee. We also defined two methods, setName and getName for this class. It is important to note the differences between the normal Python functions and class methods defined above. Each method of the class must take the same instance of the class(object) from which it was called as the first argument. It is conventionally given the name, 
       
    45     <span class="emphasis"><em>self</em></span>. Note that 
       
    46     <span class="emphasis"><em>self</em></span> is only a convention. You can use any other name, but the first argument to the method will always be the same object of the class from which the method was called. The data memebers that belong to the class are called as 
       
    47     <span class="emphasis"><em>class attributes</em></span>. 
       
    48     <span class="emphasis"><em>Class attributes</em></span> are preceded by the object of the class and a dot. In the above example, 
       
    49     <span class="emphasis"><em>name</em></span> is a class attribute since it is preceded by the 
       
    50     <span class="emphasis"><em>self</em></span> object. 
       
    51     <span class="emphasis"><em>Class attributes</em></span> can be accessed from anywhere within the class.
       
    52   </p>
       
    53 <p><a name="x_c5"></a>We can create objects of a class outside the class definition by using the same syntax we use to call a function with no parameters. We can assign this object to a variable:</p>
       
    54 <pre class="programlisting">emp = Employee()
       
    55 
       
    56 </pre>
       
    57 <p><a name="x_c6"></a>In the above example, we create an object named 
       
    58     <span class="emphasis"><em>emp</em></span> of the class 
       
    59     <span class="emphasis"><em>Employee</em></span>. All the attributes and methods of the class can be accessed by the object of the class using the standard notation 
       
    60     <span class="emphasis"><em>object.attribute</em></span> or 
       
    61     <span class="emphasis"><em>object.method()</em></span>. Although the first parameter of a class method is the self object, it must not be passed as an argument when calling the method. The 
       
    62     <span class="emphasis"><em>self</em></span> object is implicitly passed to the method by the Python interpreter. All other arguments passing rules like default arguments, keyword arguments, argument packing and unpacking follow the same rules as those for ordinary Python functions:
       
    63   </p>
       
    64 <pre class="programlisting">&gt;&gt;&gt; emp.setName('John')
       
    65 &gt;&gt;&gt; name = emp.getName()
       
    66 &gt;&gt;&gt; print name
       
    67 John
       
    68 &gt;&gt;&gt; print emp.name
       
    69 John
       
    70 
       
    71 </pre>
       
    72 <p><a name="x_c7"></a>If we at all try to access a class attribute before assigning a value to it, i.e before creating it, Python raises the same error as it would raise for the accessing undefined variable:</p>
       
    73 <pre class="programlisting">&gt;&gt;&gt; emp = Employee()
       
    74 &gt;&gt;&gt; emp.name
       
    75 Traceback (most recent call last):
       
    76   File "class.py", line 10, in &lt;module&gt;
       
    77     print e.name
       
    78 AttributeError: Employee instance has no attribute 'name'
       
    79 
       
    80 </pre>
       
    81 </div></body>
       
    82 </html>