Abdullah Diab’s Blog

I’m Learning Python part 9

I’m Learning Python part 9

<p>
  <img title="Python Logo" src="http://www.python.org/images/python-logo.gif" alt="Python Logo" /></div> 
  
  <h1 style="font-family:Georgia;">
    Classes In Python
  </h1>
  
  <div dir="ltr">
    As I told you before, Python is a powerful programming language, and powerful it won&#8217;t be without Object Oriented design.
  </div>
  
  <div dir="ltr">
    Python supports both Pure Object Oriented Programming (Every thing must be in a class) and Structural Programming (You can type code everywhere).
  </div>
  
  <div dir="ltr">
    It supports also other programming paradigms like Functional Programming. I won&#8217;t write about OOP and its uses, why we should use it, I&#8217;ll just give you the keys to use OOP in Python.
  </div>
  
  <h1 style="font-family:Georgia;">
    Defining a Class
  </h1>
  
  <p>
    Classes in Python are defined like this:
  </p>
  
  <p>

  <pre class="lang:python decode:true">

class CLASS_NAME (BASE0,BASE1,BASE2): “““OPTIONAL DOC STRING””” #Class Code . . .

  <div dir="ltr">
    Yes, I know what you&#8217;re thinking of, Python supports multiple inheritance.
  </div>
  
  <div dir="ltr">
    In Python 3.0 you must at least inherit the object class (or any other class that inherits object), this is called new-style-classes, and that&#8217;s because before Python 3.0 you could make a classic class that doesn&#8217;t inherit any class, not even object.
  </div>
  
  <div dir="ltr">
    Because of Python&#8217;s dynamic data typing you don&#8217;t need interfaces in Python, so there is no implicit syntax for declaring interfaces.
  </div>
  
  <h1 style="font-family:Georgia;">
    Sample
  </h1>
  
  <div dir="ltr">
    Let&#8217;s create a sample class, I always implement the stack when I want to show OOP examples, I don&#8217;t know why but that&#8217;s what I&#8217;m gonna do now:
  </div>
  
  <div dir="ltr">
    I always start implementing stack by implementing its unit type, a class called StackItem.
  </div>
  
  <div dir="ltr">
    StackItem is a stack unit that has two properties, its value, and a reference to its next item in the stack, so it will be defined:
  </div>
  
  <pre class="lang:python decode:true">

class StackItem(object): def init(self, value): self.Value = value self.Next = None

@property
def Value(self):
    return self.__value
@Value.setter
def Value(self, value):
    self.__value = value

@property
def Next(self):
    return self.__next
@Next.setter
def Next(self, value):
    self.__next = value

  <p>
    Let&#8217;s describe the whole scene of code:
  </p>
  
  <ol>
    <li>
      StackItem inherits object, it is a way of saying &#8220;Everything is an object&#8221; 🙂
    </li>
    <li>
      __init__ is the constructor of StackItem, it takes two arguments, the instance of StackItem to construct and the value of it, it sets the given value to the Value property of the given StackItem instance. And it sets the Next property of the given StackItem instance to None (Which is the null pointer).
    </li>
    <li>
      @property is called function decorator, a decorator is a function which takes a function as a parameter and returns a function too. (Weird but handy).<br /> The decorator works as a transformer that transforms the function from one state to another. @property decorator creates a property from the given function, a property is like a variable, you store values in it for further reading/writing, but properties have a special function called a getter that returns the hidden value, and a special function called a setter that sets a given value to the hidden variable, the benefits you get from using properties instead of variables is that you can do some check before assigning the value to the variable, you can make sure that the give value is a positive number for instance.<br /> @property created a property called Value inside StackItem class, and it bounded its value to a private variable called __value.<br /> You&#8217;d ask how come __value is a private variable?<br /> The answer is that in Python there is no access modifiers but Python annotates that any class variable with two leading underscores at least and one trailing underscore at most will be a private class variable, so other classes can&#8217;t see it, so it is a way of creating private variables.
    </li>
    <li>
      @Value.setter is another decorator which binds the setting of the Value property tho the given function.
    </li>
    <li>
      Next is like Value, a property.<br /> A property can be used later by calling its name only, just like any variable: e.g.:</p> <pre class="lang:python decode:true">

s.Value = 5 #This will call the function with #@Value.setter decorator, #passes s as the first argument, #and 5 as the second one. print(s.Value) This will call the function with #@property decorator, #passes s as the first argument.

    <li>
      self is passed to each function in the class, it refers to the instance of the class that the function was called from (Like this in C++, C#, Java and Me in Visual Basic).<br /> When not passing self to the function you&#8217;re declaring that this function doesn&#8217;t need an instance of the class to be called, in other words, you&#8217;re declaring this as a static function, but you must tell the compiler that you did it on purpose so you should add @staticmethod decorator to the function, e.g.:</p> <pre class="lang:python decode:true">

class C(object): def NonStatic(self): print (“NonStatic” ) @staticmethod def Static(): print(“Static” )

cObj = C() cObj.NonStatic() #Will print NonStatic #(self = cObj) cObj.Static() #Will print Static C.Static() #Will also print Static C.NonStatic() #Would give an error because #NonStatic expects 1 argument #and was called using none.

  <p>
    The other part must be easy for you after understanding the first one, now we&#8217;ll implement the Stack class:
  </p>
  
  <pre class="lang:python decode:true">

class Stack(object): @property def Head(self): return self.__head

@property
def Count(self):
    return self.__count

def __init__(self):
    self.__head = None
    self.__count = 0

def Push(self, value):
    if (self.__head is None):
        self.__head = StackItem(value)
    else:
        temp = StackItem(value)
        temp.Next = self.__head
        self.__head = temp
    self.__count += 1

def Pop(self):
    if (self.__head is None):
        raise Exception("Empty Stack" )
    else:
        temp = self.__head
        self.__head = self.__head.Next
        self.__count -= 1
        return temp.Value

def PopAll(self):
    while (self.__count &gt; 0):
        yield self.Pop()

  <p>
    Notes:
  </p>
  
  <ol>
    <li>
      Stack has two read-only properties (They have no setters), Head (a reference to its top-most item) and Count (the count of items in the Stack).
    </li>
    <li>
      The constructor sets the Count to 0 and the Head to None.
    </li>
    <li>
      Push takes two arguments, the stack to push into, and the value to be pushed.
    </li>
    <li>
      Pop takes one argument, the stack to pop out from, and returns the popped value. If the stack is empty Pop method will raise (throw) a new Exception with a message that says &#8220;Empty Stack&#8221;.
    </li>
    <li>
      PopAll is called a Generator, it generates values from a data structure, it can be used with for loops. e.g.: <pre class="lang:python decode:true">

for value in s.PopAll(): print(value)

      <p>
        yield is a keyword in Python that yields 😀 a given value to the caller. PopAll method works this way: The first time the caller calls it it would return the first value and stop. Each next time it will continue from where it stopped.</li> </ol> 
        
        <p>
          Now we can use our stack freely 🙂
        </p>
        
        <pre class="lang:python decode:true">

S = Stack()

map(S.Push, range(1, 10))

for value in S.PopAll(): print value

        <div dir="ltr">
          Simple code, right?
        </div>
        
        <div dir="ltr">
          Notes:
        </div>
        
        <ol>
          <li>
            S = Stack() initializes a new Stack and put its reference in S.
          </li>
          <li>
            map is a function that takes a function as first argument and a list as second argument, and it calls the given function on each value from the give list and returns a tuple with the values of calling the function, in our case there is no return because Push returns no value. range is a function that takes two numbers and returns a list of numbers between the first one inclusively and the second one exclusively. e.g.: <pre class="lang:python decode:true">

print(rang(1, 10)) #prints from 1 to 9

            <p>
              range can also take three numbers, the third would be the step between each two numbers. e.g.:
            </p>
            
            <pre class="lang:python decode:true">

print(range(1, 10, 2)) #prints 1, 3, 5, 7, 9

            <p>
              So map(S.Push, range(1, 10)) will fill the stack with values from 1 to 9.</li> 
              
              <li>
                I used the PopAll method which is a generator to iterate over the StackItems and print them.
              </li></ol> 
              
              <h1 style="font-family:Georgia;">
                Overriding and Overloading
              </h1>
              
              <div dir="ltr">
                In OOP we use Overriding for a method in the sub-class that overrides a method in the base-class, and Overloading for two methods with the same name but differ in signature (number of arguments, types of arguments, order of arguments).
              </div>
              
              <div dir="ltr">
                Usually to override a method in the sub-class the method must be declared virtual in the base-class, but in Python all methods are implicitly virtual.
              </div>
              
              <div dir="ltr">
                To override a method you simply have to define it in the sub-class. e.g:
              </div>
              
              <pre class="lang:python decode:true">

class A(object): def M(self): print (self.X) def init(self): self.X = 5 class B(A): def M(self): print (self.X + 1)

b = B() b.M() #Will print 6

              <div dir="ltr">
                Sometimes you need to extend the base method instead of overriding it completely, you can call the base method by using BaseClassName.MethodName(self, arguments).
              </div>
              
              <div dir="ltr">
                e.g.:
              </div>
              
              <pre class="lang:python decode:true">

class C(B): def M(self): B.M(self) print (“This is C” ) c = C() c.M() #Will print 6 This is C

              <div dir="ltr">
                While overriding is a piece of cake, overloading is not supported by Python, you have to pick different names for different overloads from your method, that is because Python associates each function to a variable, so when overloading the variable will have the last function only.
              </div>
              
              <div dir="ltr">
                A def statement is roughly executed like this:
              </div>
              
              <ol>
                <li>
                  Compile the body of the function.
                </li>
                <li>
                  Build a function object __f
                </li>
                <li>
                  Assign it: FunctionName = __f
                </li>
              </ol>
              
              <h1 style="font-family:Georgia;">
                Bottom Line:
              </h1></div> 
              
              <div dir="ltr">
                Python supports Pure Object Oriented strongly, You can write pure Object Oriented programs using Python, so why don&#8217;t you start writing your programs now?
              </div>
              
              <div dir="ltr">
                Cheers.
              </div>