Understanding Python Modules, Packages, Classes, and Inheritance
This guide explains Python modules, packages, class definitions, object creation, inheritance, and polymorphism, providing clear examples and code snippets to illustrate how to organize code, import components, and apply object‑oriented principles in practice.
Python modules are files that contain related functions, classes, and statements, which can be imported and reused in other Python files.
Creating a module
# mymodule.py
def greet(name):
return f"你好, {name}!"
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"我的名字是 {self.name},我今年 {self.age} 岁。"Importing a module
import mymodule
print(mymodule.greet("Alice")) # 输出: 你好, Alice!
person = mymodule.Person("Bob", 25)
print(person.introduce()) # 输出: 我的名字是 Bob,我今年 25 岁。Importing specific objects
from mymodule import greet, Person
print(greet("Alice")) # 输出: 你好, Alice!
person = Person("Bob", 25)
print(person.introduce()) # 输出: 我的名字是 Bob,我今年 25 岁。Using aliases
import mymodule as mm
print(mm.greet("Alice")) # 输出: 你好, Alice!
from mymodule import Person as P
person = P("Bob", 25)
print(person.introduce()) # 输出: 我的名字是 Bob,我今年 25 岁。Python packages are directories containing multiple modules and an __init__.py file that marks the directory as a package.
Creating a package
mypackage/
__init__.py
module1.py
module2.py
# module1.py
def func1():
return "这是来自 module1"
# module2.py
def func2():
return "这是来自 module2"
# __init__.py (optional)
from .module1 import func1
from .module2 import func2Importing a package
import mypackage
print(mypackage.func1()) # 输出: 这是来自 module1
print(mypackage.func2()) # 输出: 这是来自 module2Importing modules from a package
from mypackage import module1, module2
print(module1.func1()) # 输出: 这是来自 module1
print(module2.func2()) # 输出: 这是来自 module2Classes define user‑created data types that encapsulate attributes and methods, forming the basis of object‑oriented programming.
Defining a class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"我的名字是 {self.name},我今年 {self.age} 岁。"
def get_age(self):
return self.age
def set_age(self, new_age):
if new_age > 0:
self.age = new_age
else:
print("年龄必须是正数。")Creating objects
person1 = Person("Alice", 30)
print(person1.introduce()) # 输出: 我的名字是 Alice,我今年 30 岁。
person2 = Person("Bob", 25)
print(person2.introduce()) # 输出: 我的名字是 Bob,我今年 25 岁。Accessing attributes and methods
print(person1.name) # 输出: Alice
print(person1.get_age()) # 输出: 30
person1.set_age(35)
print(person1.get_age()) # 输出: 35
person1.set_age(-10) # 输出: 年龄必须是正数。Inheritance allows a subclass to acquire attributes and methods from a parent class.
Defining a subclass
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def study(self):
return f"{self.name} 正在学习。"
def introduce(self):
return f"我的名字是 {self.name},我今年 {self.age} 岁,我的学号是 {self.student_id}。"Creating a subclass object
student1 = Student("Charlie", 20, "S12345")
print(student1.introduce()) # 输出: 我的名字是 Charlie,我今年 20 岁,我的学号是 S12345。
print(student1.study()) # 输出: Charlie 正在学习。Polymorphism enables different classes to be used interchangeably through a common interface.
Polymorphism example
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age)
self.subject = subject
def teach(self):
return f"{self.name} 正在教 {self.subject}。"
def introduce(self):
return f"我的名字是 {self.name},我今年 {self.age} 岁,我教 {self.subject}。"
def introduce_person(person):
print(person.introduce())
person1 = Person("Alice", 30)
student1 = Student("Charlie", 20, "S12345")
teacher1 = Teacher("David", 40, "数学")
introduce_person(person1) # 我的名字是 Alice,我今年 30 岁。
introduce_person(student1) # 我的名字是 Charlie,我今年 20 岁,我的学号是 S12345。
introduce_person(teacher1) # 我的名字是 David,我今年 40 岁,我教 数学。Summary
Modules are Python files that group related functionality.
Packages are directories of modules, identified by an __init__.py file.
Classes define custom data types that encapsulate data and behavior.
Objects are instances of classes.
Inheritance lets a class reuse attributes and methods from another class.
Polymorphism allows different class objects to be used through the same interface, producing varied behavior.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.