__init__
的本质是什么?Python文档描述:
“
__init__
is called after the instance has been created by__new__()
, and is responsible for initializing the new object’s state.”
特性 | 说明 |
---|---|
初始化方法 | 负责初始化对象状态 |
非构造函数 | 实际对象创建由__new__ 完成 |
自动调用 | 在实例化时自动执行 |
无返回值 | 必须返回None |
self参数 | 第一个参数指向当前实例 |
class MyClass:
def __new__(cls, *args, **kwargs):
"""实际创建对象(构造函数)"""
print("__new__ 执行:创建实例")
return super().__new__(cls)
def __init__(self, value):
"""初始化对象(构造器)"""
print("__init__ 执行:初始化属性")
self.data = value
obj = MyClass(42)
# 输出:
# __new__ 执行:创建实例
# __init__ 执行:初始化属性
__init__
的工作机制class Person:
# 方式1:必需参数初始化
def __init__(self, name):
self.name = name
# 方式2:带默认值的参数
def set_age(self, age=30):
self.age = age
# 方式3:类常量关联
MIN_AGE = 18
def __init__(self, name, age):
self.name = name
self.age = max(age, self.MIN_AGE)
# 创建实例
p1 = Person("Alice") # 必需参数
p1.set_age() # 默认参数: age=30
p2 = Person("Bob", 16) # 类常量: age=18
class FileProcessor:
def __init__(self, file_path, **kwargs):
"""通用初始化方法"""
self.file_path = file_path
# 动态添加属性
for key, value in kwargs.items():
setattr(self, key, value)
# 子类定制化
class CSVProcessor(FileProcessor):
def __init__(self, file_path, delimiter=',', encoding='utf-8'):
"""CSV专用初始化"""
super().__init__(file_path, delimiter=delimiter, encoding=encoding)
# 使用
csv_processor = CSVProcessor("data.csv", delimiter=';')
print(csv_processor.delimiter) # ;
__init__
的五大核心功能class BankAccount:
def __init__(self, account_holder, initial_balance=0):
"""设置账户初始状态"""
self.holder = account_holder
self.balance = initial_balance
self.transactions = []
self.active = True
print(f"账户创建成功: {self.holder}")
# 创建账户
account = BankAccount("Alice", 1000)
print(f"余额: ${account.balance}")
class TemperatureSensor:
ABSOLUTE_ZERO = -273.15
def __init__(self, temp_celsius):
"""温度验证与转换"""
if temp_celsius < self.ABSOLUTE_ZERO:
raise ValueError("温度不能低于绝对零度")
self.celsius = temp_celsius
self.fahrenheit = (temp_celsius * 9/5) + 32
try:
sensor = TemperatureSensor(-300)
except ValueError as e:
print(f"错误: {e}") # 温度不能低于绝对零度
class Department:
def __init__(self, name):
self.name = name
self.employees = []
class Employee:
def __init__(self, name, department):
self.name = name
self.department = department
# 双向关联
department.employees.append(self)
# 构建关联
tech_dept = Department("技术部")
emp1 = Employee("Alice", tech_dept)
emp2 = Employee("Bob", tech_dept)
print([e.name for e in tech_dept.employees]) # ['Alice', 'Bob']
print(emp1.department.name) # 技术部
class ShoppingCart:
MAX_ITEMS = 100
def __init__(self, user):
self.user = user
self.items = []
self._discount = 0 # 私有变量
def apply_discount(self, code):
"""应用折扣业务规则"""
if code == "WELCOME10":
self._discount = 0.1
elif code == "VIP25":
if self.user.is_vip:
self._discount = 0.25
# 其他规则...
class DatabaseConnection:
def __init__(self, connection_string):
"""延迟资源初始化"""
self.connection_string = connection_string
self._connection = None # 实际连接延迟创建
@property
def connection(self):
if self._connection is None:
print("建立数据库连接...")
self._connection = self._create_connection()
return self._connection
def _create_connection(self):
# 模拟创建连接
return f"Connection to {self.connection_string}"
# 使用
db = DatabaseConnection("localhost:5432")
# 需要时才真正创建连接
print(db.connection) # 建立数据库连接... Connection to localhost:5432
__init__
class Animal:
def __init__(self, species):
self.species = species
print(f"Animal __init__: {species}")
class Mammal(Animal):
def __init__(self, species, is_domesticated):
# 正确方式:super()调用父类
super().__init__(species)
self.is_domesticated = is_domesticated
print(f"Mammal __init__: {species}")
# 创建实例
dog = Mammal("Canine", True)
# 输出:
# Animal __init__: Canine
# Mammal __init__: Canine
class Camera:
def __init__(self, resolution):
self.resolution = resolution
print("Camera initialized")
class Phone:
def __init__(self, number):
self.number = number
print("Phone initialized")
class SmartPhone(Camera, Phone):
def __init__(self, resolution, number, os):
# MRO顺序调用父类
Camera.__init__(self, resolution)
Phone.__init__(self, number)
self.os = os
print("SmartPhone initialized")
# 创建实例
iphone = SmartPhone("12MP", "123-456", "iOS")
# 输出:
# Camera initialized
# Phone initialized
# SmartPhone initialized
class Singleton:
_instance = None
def __new__(cls, *args, **kwargs):
"""控制实例创建"""
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self, config):
"""只初始化一次"""
if not hasattr(self, '_initialized'):
self.config = config
self._initialized = True
print("单例已初始化")
# 测试
s1 = Singleton({"key": "value1"}) # 单例已初始化
s2 = Singleton({"key": "value2"})
print(s1 is s2) # True
print(s1.config) # {'key': 'value1'}
class ConfigurableObject:
DEFAULT_SETTINGS = {'color': 'blue', 'size': 'medium'}
def __init__(self, **kwargs):
"""合并默认和自定义设置"""
self.settings = {**self.DEFAULT_SETTINGS, **kwargs}
print(f"创建对象: {self.settings}")
# 创建不同配置对象
obj1 = ConfigurableObject() # {'color': 'blue', 'size': 'medium'}
obj2 = ConfigurableObject(color='red') # {'color': 'red', 'size': 'medium'}
obj3 = ConfigurableObject(size='large', material='metal')
# {'color': 'blue', 'size': 'large', 'material': 'metal'}
class BaseModel:
def __init__(self, **kwargs):
"""动态设置模型属性"""
for key, value in kwargs.items():
setattr(self, key, value)
def save(self):
"""保存到数据库"""
attrs = vars(self)
print(f"保存对象: {type(self).__name__} {attrs}")
class User(BaseModel):
FIELDS = ['username', 'email']
def __init__(self, **kwargs):
"""验证必填字段"""
super().__init__(**kwargs)
missing = [f for f in self.FIELDS if not hasattr(self, f)]
if missing:
raise ValueError(f"缺少必填字段: {missing}")
# 使用
try:
user = User(username="alice") # 缺少email
except ValueError as e:
print(e) # 缺少必填字段: ['email']
valid_user = User(username="bob", email="[email protected]")
valid_user.save() # 保存对象: User {'username': 'bob', 'email': '[email protected]'}
None
class ErrorClass:
def __init__(self):
"""错误:返回非None值"""
return "初始化完成" # 导致TypeError
# 正确方式:删除返回语句
class FixedClass:
def __init__(self):
pass # 隐式返回None
class HeavyObject:
def __init__(self):
"""错误:在__init__中执行耗时操作"""
self.load_huge_data() # 拖慢实例化速度
self.initialize_network() # 可能失败
def load_huge_data(self):
print("加载大量数据...")
def initialize_network(self):
print("初始化网络连接...")
# 正确方式:采用延迟初始化
class OptimizedObject:
def __init__(self):
"""仅设置状态标记"""
self._data_loaded = False
def load_data(self):
"""按需加载数据"""
if not self._data_loaded:
print("按需加载大量数据...")
self._data_loaded = True
super
class Base:
def __init__(self):
self.base_value = 10
class Derived(Base):
def __init__(self):
"""错误:忘记调用super"""
self.derived_value = 20
d = Derived()
print(hasattr(d, 'base_value')) # False,基类未初始化
# 正确方式:显式调用super
class CorrectDerived(Base):
def __init__(self):
super().__init__()
self.derived_value = 20
cd = CorrectDerived()
print(cd.base_value) # 10
__init__
最佳实践原则 | 应用 |
---|---|
单一职责 | __init__ 仅负责初始化 |
最小惊讶 | 避免在__init__ 中执行复杂操作 |
防御性编程 | 验证输入参数 |
明确命名 | 使用明确参数名增强可读性 |
延迟加载 | 对耗时资源按需初始化 |
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
class ProductFactory:
"""工厂方法简化复杂对象创建"""
@classmethod
def create_book(cls, title, author, price):
"""创建书籍产品"""
return Product(f"Book: {title} by {author}", price)
@classmethod
def create_electronics(cls, brand, model, price):
"""创建电子产品"""
return Product(f"Electronics: {brand} {model}", price)
# 使用工厂
book = ProductFactory.create_book("Python Guide", "A. Expert", 29.99)
laptop = ProductFactory.create_electronics("Dell", "XPS 15", 1499.99)
print(book.name) # Book: Python Guide by A. Expert
print(laptop.price) # 1499.99
class Vector:
__slots__ = ('x', 'y', 'z') # 减少内存占用
def __init__(self, x=0.0, y=0.0, z=0.0):
"""使用__slots__优化内存"""
self.x = x
self.y = y
self.z = z
class CachedInstance:
"""缓存常用参数组合"""
_cache = {}
def __new__(cls, config):
"""缓存模式减少重复初始化"""
if config in cls._cache:
print("返回缓存实例")
return cls._cache[config]
instance = super().__new__(cls)
cls._cache[config] = instance
return instance
def __init__(self, config):
if not hasattr(self, 'config'):
print(f"创建新实例:{config}")
self.config = config
# 初始化...
# 测试
v1 = Vector() # 内存优化实例
c1 = CachedInstance("A")
c2 = CachedInstance("A") # 返回缓存实例
c3 = CachedInstance("B") # 创建新实例:B
__init__
vs __new__
特性 | __init__ |
__new__ |
---|---|---|
功能 | 初始化已有对象 | 创建新对象 |
参数 | self 指向实例 |
cls 指向类 |
返回值 | 必须返回None |
必须返回实例对象 |
使用频率 | 几乎每个类都会使用 | 特殊场景使用 |
__init__
vs __del__
python
复制
class LifecycleDemo:
def __init__(self, name):
print(f"创建对象: {name}")
self.name = name
def __del__(self):
print(f"销毁对象: {self.name}")
def test():
obj = LifecycleDemo("临时对象")
print("函数结束,对象将销毁")
test() # 创建对象: 临时对象
# 函数结束,对象将销毁
# 销毁对象: 临时对象
__init__
的核心价值__init__
是初始化方法,不是构造函数__new__
创建对象后自动调用self
参数访问和操作新实例super()
正确调用父类“
__init__
体现了Python的’显式优于隐式’原则。通过明确定义初始化过程,它让对象的创建和状态设置变得可控和可预测。”
通过本教程,您应该掌握:
__init__
方法初始化对象状态“好的
__init__
设计是Python类设计的基石,它为对象生命周期的开始提供了清晰、可靠的起点。” - Python核心开发指南