Xin chào các bạn. Hôm nay mình sẽ giới thiệu với các bạn về Class và việc Phân chia Module trong Python
- Class
- Khai báo một Class
- Phân chia module
- Các loại module / thư viện
- Đường dẫn tìm để load module
- Lấy danh sách thuộc tính và phương thức của một module
- Cách khai báo và sử dụng module
- Package module
Class
Lập trình hướng đối tượng là một khái niệm không thể thiếu trong hầu hết các ngôn ngữ thông dụng hiện nay. Python cũng hỗ trợ lập trình hướng đối tượng với các khái niệm Class, Object, Override...
Khai báo một Class
Phân chia module
class myclass([parentclass]): assignments def __init__(self): statements def method(): statements def method2(): statementsCác loại module / thư viện
class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go()Đường dẫn tìm để load module
My Name is Animal is running... Animal is going... My Name is Lucy Dog is running... Animal is going...Lấy danh sách thuộc tính và phương thức của một module
- Cách khai báo và sử dụng module
- Package module
- Class
- Lập trình hướng đối tượng là một khái niệm không thể thiếu trong hầu hết các ngôn ngữ thông dụng hiện nay. Python cũng hỗ trợ lập trình hướng đối tượng với các khái niệm Class, Object, Override...
- Khai báo một class theo cú pháp sau:
Phân chia module
Các loại module / thư viện
Đường dẫn tìm để load module
Các loại module / thư viện
Đường dẫn tìm để load module
- Lấy danh sách thuộc tính và phương thức của một module
- Cách khai báo và sử dụng module
- Package module
Đường dẫn tìm để load module
Lấy danh sách thuộc tính và phương thức của một module
import modulenameCách khai báo và sử dụng module
- Package module
- Class
- Lập trình hướng đối tượng là một khái niệm không thể thiếu trong hầu hết các ngôn ngữ thông dụng hiện nay. Python cũng hỗ trợ lập trình hướng đối tượng với các khái niệm Class, Object, Override...
Khai báo một class theo cú pháp sau:
import math math.__file__ (Ví dụ trả về '/usr/lib/python2.5/lib-dynload/math. so') import random random.__file__ (Ví dụ trả về '/usr/lib/python2.5/random.pyc')Lấy danh sách thuộc tính và phương thức của một module
Cách khai báo và sử dụng module
dir(math) ['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degree s', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'fa ctorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma' , 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'lo g', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians ', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']Package module
Cách khai báo và sử dụng module
Package module
def cong(a, b): return a + b def tru(a, b): return a - b def nhan(a, b): return a * bClass
import mymath num1 = 1 num2 = 2 print 'Tong hai so la: ', mymath.cong(num1, num2)Lập trình hướng đối tượng là một khái niệm không thể thiếu trong hầu hết các ngôn ngữ thông dụng hiện nay. Python cũng hỗ trợ lập trình hướng đối tượng với các khái niệm Class, Object, Override...
$ python myexample.pyKhai báo một class theo cú pháp sau:
Tong hai so la: 3Package module
Class
Lập trình hướng đối tượng là một khái niệm không thể thiếu trong hầu hết các ngôn ngữ thông dụng hiện nay. Python cũng hỗ trợ lập trình hướng đối tượng với các khái niệm Class, Object, Override...
class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 0Khai báo một class theo cú pháp sau:
class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 1Ví dụ một class:
class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 2Ví dụ một class:
class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 3Sau khi thực thi sẽ cho ra kết quả:
Trong ví dụ trên thì:
class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 4class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 5 và class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 6 là 2 class. Trong đó class class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 6 kế thừa từ class cha là class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 5 nên sẽ có các phương thức của class class animal(): name = '' name = '' age = 0 def __init__(self, name = '', age = 0): self.name = name self.age = age def show(self): print 'My name is ', self.name def run(self): print 'Animal is running...' def go(self): print 'Animal is going...' class dog(animal): def run(self): print 'Dog is running...' myanimal = animal() myanimal.show() myanimal.run() myanimal.go() mydog = dog('Lucy') mydog.show() mydog.run() mydog.go() 5 .
My Name is Animal is running... Animal is going... My Name is Lucy Dog is running... Animal is going... 0 và My Name is Animal is running... Animal is going... My Name is Lucy Dog is running... Animal is going... 1 là thuộc tính (Attribute) của class.
//docs.python.org/3/tutorial/modules.html#compiled-python-files
//docs.python.org/3/tutorial/classes.html