python多继承与mro

本文介绍了Python中两种不同的方法解析顺序:经典类的深度优先搜索和新式类的广度优先搜索,并通过具体示例展示了这两种搜索顺序的区别及其对多重继承的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、深度优先

#!/usr/bin/env python
class Account:
	"""bank account"""
	def __init__(self, holder):
		self.balance = 0
		self.holder = holder
	def deposit(self, amount):
		self.balance = self.balance + amount
		return self.balance
	def withdraw(self, amount):
		if amount > self.balance:
			return 'not enough money!'
		return self.balance - amount

class RemoteAccount(Account):
	"""docstring for RemoteAccount"""
	deposit_charge = 1
	def __init__(self, holder):
		super(RemoteAccount, self).__init__(holder)
	def deposit(self, amount):
		return Account.deposit(self, amount - self.deposit_charge)

class InterbankAccount(Account):
	"""docstring for InterbankAccount"""
	withdraw_charge = 1
	def __init__(self, holder):
		super(InterbankAccount, self).__init__(holder)
	def withdraw(self, amount):
		return Account.withdraw(self, amount + self.withdraw_charge)

class RemoteInterbankAccount(RemoteAccount, InterbankAccount):
	"""docstring for RemoteInterbankAccount"""
	def __init__(self, holder):
		self.holder = holder
		self.balance = 0

account = RemoteInterbankAccount('zhang3')
#print [c.__name__ for c in RemoteInterbankAccount.mro()]
print account.deposit(12)
#>>> 11
print account.withdraw(10)
#>>> 1


		

2、广度优先

#!/usr/bin/env python
class Account(object):
	"""bank account"""
	def __init__(self, holder):
		self.balance = 0
		self.holder = holder
	def deposit(self, amount):
		self.balance = self.balance + amount
		return self.balance
	def withdraw(self, amount):
		if amount > self.balance:
			return 'not enough money!'
		return self.balance - amount

class RemoteAccount(Account):
	"""docstring for RemoteAccount"""
	deposit_charge = 1
	def __init__(self, holder):
		super(RemoteAccount, self).__init__(holder)
	def deposit(self, amount):
		return Account.deposit(self, amount - self.deposit_charge)

class InterbankAccount(Account):
	"""docstring for InterbankAccount"""
	withdraw_charge = 1
	def __init__(self, holder):
		super(InterbankAccount, self).__init__(holder)
	def withdraw(self, amount):
		return Account.withdraw(self, amount + self.withdraw_charge)

class RemoteInterbankAccount(RemoteAccount, InterbankAccount):
	"""docstring for RemoteInterbankAccount"""
	def __init__(self, holder):
		self.holder = holder
		self.balance = 0

account = RemoteInterbankAccount('zhang3')
print [c.__name__ for c in RemoteInterbankAccount.mro()]
#>>> ['RemoteInterbankAccount', 'RemoteAccount', 'InterbankAccount', 'Account', 'object']
print account.deposit(12)
#>>> 11
print account.withdraw(10)
#>>> 0
		

我们通常称深度优先为classic,广度优先为new-style——基类需继承object。广度优先基于C3 Method Resolution Ordering方法,该方法对应于Python中的mro方法。多重继承是一个无法标准化的问题,每一种提供多重继承的语言都有自己的继承方式,我们只需遵循这种特定方式就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值