cout << "Hello, World! \ N" in python

I wrote it in Comment of this article, but I will post it because it is a big deal. This is a method to make cout <<, which is familiar in C ++, also available in python.

import sys

class ConsoleOut(object):
    def __lshift__(self, message):  # Left Shift(<<)Define operator processing
        sys.stdout.write(str(message))
        return self

cout = ConsoleOut()

with this,

cout << "Hello, World!\n"

You will be able to write.

You can also save the above definition in a file called ConsoleOut.py and use it as follows:

In the shell


$ python
Python 2.7.10 (default, Jun  1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ConsoleOut import cout
>>> cout << "Hello, World!\n"
Hello, World!
>>>

Strings are concatenate with +, iterate with *, and format format with %, but each processing method of the string (str) class is implemented so. That's right. To see what other methods are available, run help (object) or help (str) in the python interpreter.

Recommended Posts

cout << "Hello, World! \ N" in python
How to display Hello world in python
Hello World in GO language
Python starting with Hello world!
Let's do "Hello World" in 40 languages! !!
[Note] Hello world output with python
Hello World in Flask [Appropriate memo]
Code: 2 "Hello World" in "Choregraphe-Python script"
Hello world
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
Python #Hello World for super beginners
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Python beginners tried Hello World in 30 seconds using the micro-framework Flask
In Python 3.8, pow (n, -1, 1000000007) looks better than pow (n, 1000000007-2, 1000000007)
[Python] Web application from 0! Hands-on (2) -Hello World-
Hello world instead of localhost in Django
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Pymacs hello world
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
cython hello world
Flatten in python
flatten in python
Hello World and face detection with OpenCV 4.3 + Python