In the first week we reviewed some basic knowledge from CSC108. Then we took the class Point and class stack as example to learn more details about class in python.
Notes:
1. We can use loop in a list, e.g. If we what to create a list which contain all square number from integer 1 to 9, we can write:
>>> s = [x**x for x in range(1, 10)]
By this method we can calculate a set of objects but not implement them one by one.We can also use function in this method:
>> def square(x):
return x**2
>>> s = [square(x) for x in range(1, 10)]
2. The minimum need to create a class in python is the name of the class, e.g.
>>> class Point():
Pass
3. There are some special methods for class:
__init__ : initialize the class e.g.
def __init__(self, coord):
""" (Point, list-of-floats) -> NoneType
Create and initialize this point.
>>> p = Point([3, 4])
"""
self.coord = [float(x) for x in coord]
__repr__ : Use a str to represent a class instance e.g.
def __repr__(self):
""" (Point) -> str
Return a string representation of self that evaluates
into an equivalent Point.
>>> p = Point([3, 4])
>>> p
Point([3.0, 4.0])
"""
return "Point({})".format(self.coord)
__str__: sometimes we may need more specific words to describe the class, so we may use __str__ method. If we dont state it, python will use __repr__ to represent the class instance.
def __str__(self):
""" (Point) -> strReturn a string version of self.>>> p = Point([3, 4]) >>> print(p) (3.0, 4.0)
"""""" (Point, object) -> boolreturn str(tuple(self.coord))_eq_ : Being used for check whether two objects are same. e.g.def __eq__(self, other):
Return whether self is equivalent to other.
>>> p1 = Point([3, 4, 5])
>>> p2 = Point([3.0, 4.0, 5.0])
>>> p1 == p2
True
"""
return (isinstance(other, Point) and self.coord == other.coord)
Week2
The topic in week 2 is stacks. We can create a class named Stacks to hold the elements. There are four basic method in Staks:
class Stack: def __init__(self): self.items = [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def is_empty(self): return (self.items == [])
__init__: initialize an empty stack(an empty list).
pop: remove the top item in the stack.
push(n): add the element n to the top of the stack.
is_empty: check whether the stack is empty.
We can use some method in the stack, for example:
def adding(n):
'''(str) -> int
Pre-condition: n contains a math symbol in '+-*/'.
Return the sum all numbers in n.
>>>n = '123456789'
>>>adding(n)
579
'''
k = Stack()
for x in n:
k.push(x)
for i in k.items:
Week3
In the third week we studied how to build a sub class base on a main class. A sub class will inherit all method from the main class also we can add new method to sub class. If same method exist in both sub class and main class (e.g.__repr__, __str__, __init__), python will implement the closer method.
To use the methods from main class:
class A:
def square(x):
return x ** 2
class B(A):
def double_square:
return (A.square(x)) * 2
To import a specific class from another file:
from file import A
class B(A):
Pass
If a method in main class cannot be state but can be stated in sub class, we can raise a Error:
class A:
def double_square(x):
raise ValueError
No comments:
Post a Comment