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