Python Cookbook-2.13 使用C++的类iostream语法

任务

C++的基于 ostream 和操纵符(插入了这种特定的对象后,它会在 stream 中产生特定的效果)的 I/O方式,并想将此形式用在自己的Python 程序中。

解决方案

Python 允许使用对特殊方法(即名字前后带有连续两个下划线的方法)进行了重定义的类来重载原有的操作符。为了将<<用于输出,如同在 C++中所做的一样,需要编写一个输出流类,并定义特殊方法__lshift__:

python 复制代码
class IOManipulator(object):
	def __init__(self,function = None):
		self.function = function
	def do(self,output):
		self.function(output)
def do_endl(stream):
	stream.output.write('\n')
	stream.output.flush()
endl = IOManipulator(do_endl)
class OStream(object):
	def __init__(self,output = None):
		if output is None:
			import sys
			output = sys.stdout
			self.output =output
			self.format = '%s'
def __lshift__(self,thing):
	''' 当你使用<<操纵符并且左边操作对象是OStream时,
	Python会调用这个特殊方法'''
	if isinstance(thing,IOManipulator):
		thing.do(self)
	else:
		self.output.write(self.format % thing)
		self.format = '%s'
	return self
def example main():
	cout=OStream()
	cout<<"The average of "<<1<<" and "<<3<<" is "<<(1+3)/2 <<endl
#输出:The average of land 3is 2
if __name__== '__main__':
	example_main()

讨论

在 Python 中包装一个像文件一样的对象,模拟 C++的 ostream 的语法,还算比较容易。本节展示了怎样编写代码实现插入操作符<<的效果。解决方案中的代码实现了一个IOManipulator类(像 C++中一样)来调用插入到流中的任意函数,还实现了预定义的操纵符 endl(猜猜它得名何处)来写入新行和刷新流。

Ostream 类的实例有一个叫做 format的属性,在每次调用 self.output.write 之后,这个属性都会被设置为默认值"%s",这样的好处是,每次创建一个操纵符之后我们可在其中临时保存流对象的格式化状态,比如:

python 复制代码
def do_hex(stream):
	stream.format = '%x'
hex= IOManipulator(do_hex)
cout<<23<<'in hexis '<< hex<< 23<<',and in decimal'<<23<<endl
#输出:23 in hex is 17,and in decimal 23

些人很讨厌 C++的 cout<<something 的语法,另一些人却很喜欢。在本节的例子中所用的语法至少在可读性和简洁方面都胜过下面的语法:

python 复制代码
print>>somewhere, "The average of &d and &d is $f\n" %(1,3,(1+3)/2)

这种方式是 Python"原生"的方式(看上去很像C的风格)。这要看你更习惯 C++还是C,至少本节给了你另一个选择。即使最终没有使用本节提供的方式,了解Python中简单的操作符重载还是蛮有趣的。

相关推荐
X56611 小时前
如何在 Laravel 中正确保存嵌套动态表单数据(主服务与子服务)
jvm·数据库·python
FQNmxDG4S2 小时前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言
ZhengEnCi2 小时前
03ab-PyTorch安装教程 📚
python
前端老石人2 小时前
HTML 字符引用完全指南
开发语言·前端·html
matlab_xiaowang2 小时前
Redux 入门:JavaScript 可预测状态管理库
开发语言·javascript·其他·ecmascript
狐狐生风3 小时前
LangChain 向量存储:Chroma、FAISS
人工智能·python·学习·langchain·faiss·agentai
虹科网络安全3 小时前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
狐狐生风3 小时前
LangChain RAG 基础
人工智能·python·学习·langchain·rag·agentai
axng pmje3 小时前
Java语法进阶
java·开发语言·jvm
汉克老师3 小时前
GESP2025年3月认证C++五级( 第三部分编程题(1、平均分配))
c++·算法·贪心算法·排序·gesp5级·gesp五级