zoukankan      html  css  js  c++  java
  • Python模拟C++输出流

    看到一Python例子,挺有意思的,用Python模拟C++的输出流OStream.单纯只是玩。

    原理: 利用Python __lshift__左移内建函数<<,调用时将输出内容,如果内容为回车,则处理回车。

    看例子^_^!!!

    #coding: utf-8
    
    class IOManipulator(object):
        def __init__(self, function=None):
            self.function = function
    
        def do(self, output):
            self.function(output)
    
    # 处理回车
    def do_endle(stream): stream.output.write(' ') stream.output.flush() endl = IOManipulator(do_endle) 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): if isinstance(thing, IOManipulator): thing.do(self) else: self.output.write(self.format % thing) self.format = '%s' return self def main(): cout = OStream() cout << "abc" << 1 << "ddddd" << endl if __name__ == '__main__': main()
  • 相关阅读:
    HOW TO MAKE IT FLOW ?
    ansys14.0 从入门到精通
    ansys 14.0
    C++ GUI Qt4 编程 (第二版)
    零基础学QT编程
    医学成像与医学图像处理
    曾巩传
    c++ 编程调试秘笈
    matlab实用教程
    不要重复发明轮子-C++STL
  • 原文地址:https://www.cnblogs.com/zhuangzebo/p/4365171.html
Copyright © 2011-2022 走看看