2014年1月27日月曜日


Week 3 -csc148-

Object oriented programming
Python is an object oriented programming. This means that a powerful style pf programming in which data and the operations that are relevant for that kind of data. In object oriented programming the focus in on the creation of objects which contain  both data and functionality together. Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real world objects interact. 

When programmer represents numeric value by python, he can defines a new class. Class definitions are near the beginning after the import statements. The header begins with the keyword, class, followed by the name of the class, and ending with a colon. Every class must have a special method __init__, called initializer. Object instances have attribute, it means that one of the named data items that makes up and instance. 

Subclass and Superclass
Stack - It is an example used in Week 2.
  • We decided to extend the features of Stack. eg. Create new Stack that only accepts integers. This means that modifying the existing class.
  1. We must break the existing clients to create new methods.
  2. Cut and Paste to modify Stack. This process is hard to maintain. 
  3. New class has attributes of Stack. 
  We can avoid recursion, such as writing same codes twice, by creating a subclass. A subclass inherits all the features of a Superclass. This means that the Superclass is a parent and the Subclass is an offspring. >class Newclass(Oldclass):
  In this case, Newclass is a subclass and Oldclass is a superclass.
  We create subclasses to specialize the features of a new class by adding and modifying behaviors. We don't need to create __init__ method in subclass.
  We can access to Superclass methods by:
>OldClass.method(self,,,) 

New method isinstance(object, classinfo)
          Return true if object argument is an instance of the classinfo argument.
          Example : >isinstance(4, int)
                          True
                        >isinstance('Hello', str)
                          True

Exception 
Whenever a runtime error occurs, it creates and exception object. The program stops running if it occurs and Python describes the exception. Python programmer can create Exception class to raise exception. 

example:

def pop(self: 'IntStack') -> int:
    if self.is_empty():
        raise PopEmptyStackException   
    else:
         Stack.pop(self)

class PopNonIntegerException(Exception):
    pass

If pop(self) causes an error, then PopNonIntegerException appears. 

There are superclass and subclass of Exeptions. For example:

class SpecialException(Exception):
    pass
class ExtremeException(SpecialException):
    pass

In this case, Exception is a superclass and ExtremeException is a subclass of Exception. ExtremeException is a superclass and ExtremeException is a superclass of ExtremeException. And ExtremeException is a subclass of ExtremeException. 

__eq__
When some objects are created by initializer methods, they are different objects and their memories are different. For example:

>>>S1 = Stack()
>>>S2 = Stack()
>>>S1 == S2
     False
>>>S1.push(3)
>>>S2.push(3)
>>>S1 == S2
     False

S1 and S2 are created by class methods and their memories are different. S1 and S2 includes same numerical value. However, their memories are different. 
Then a special method is required. After I make __eq__, memories are still different. 

def __eq__(self, other):
    return is_instance(object, Stack) and self._data = other._data

__repr__
__repr__ is a special methods to represent class as a string. 
def __repr__(self: 'Stack') -> str:
    return 'Stack({ })'.format(self._data)