我会随便说,C++ 近年来开始”抄袭” Python 么?我只会说,我在用 C++ 来学习 Python.
不信?来跟着我学?
Python 早在 2.6 版本中就支持将二进制作为字面量了[1], 最近 C++14 逐步成熟,刚刚支持这么干[2]:
12 | static const int primes = 0b10100000100010100010100010101100; |
更不用说 Python 在 1.5 时代就有了 raw string literals 的概念[3],咱们 C++ 也不算晚,C++11里也有了类似做法:
12 | const char* path = r\”C:\\Python27\\Doc\”; |
Python 写 for
循环是一件非常舒畅的事情:
123 | for x in mylist: print(x); |
大家都知道了,C++11里我总算也能做同样的事情了:
12 | for (int x : mylist) std::cout << x; |
Python 中真的有类型的概念吗?(笑
123 | x = \”Hello World\”print(x) |
C++11 也学会了这招,只不过保留了老太太的裹脚布(auto
)。
12 | auto x = \”Hello World\”;std::cout << x |
Python 里的元组(tuple
)让人羡慕已久,这玩意 Python 从一开始就有了。
123 | triple = (5, \”Hello\”, True)print(triple[0]) |
好嘛,我来用 C++11 照猫画虎:
12 | auto triple = std::make_tuple(5, \”hello\”, true);std::cout << std::get<0>(triple); |
有人说了,Python 大法好,还能逆向解析成变量呢
12 | x, y, z = triple |
哼,C++难道不行?
12 | std::tie(x, y, z) = triple; |
n-sy\”>, z) = triple;
12 | static const int primes = 0b10100000100010100010100010101100; |
更不用说 Python 在 1.5 时代就有了 raw string literals 的概念[3],咱们 C++ 也不算晚,C++11里也有了类似做法:
12 | const char* path = r\”C:\\Python27\\Doc\”; |
Python 写 for
循环是一件非常舒畅的事情:
123 | for x in mylist: print(x); |
大家都知道了,C++11里我总算也能做同样的事情了:
12 | for (int x : mylist) std::cout << x; |
Python 中真的有类型的概念吗?(笑
123 | x = \”Hello World\”print(x) |
C++11 也学会了这招,只不过保留了老太太的裹脚布(auto
)。
12 | auto x = \”Hello World\”;std::cout << x |
Python 里的元组(tuple
)让人羡慕已久,这玩意 Python 从一开始就有了。
123 | triple = (5, \”Hello\”, True)print(triple[0]) |
好嘛,我来用 C++11 照猫画虎:
12 | auto triple = std::make_tuple(5, \”hello\”, true);std::cout << std::get<0>(triple); |
有人说了,Python 大法好,还能逆向解析成变量呢
12 | x, y, z = triple |
哼,C++难道不行?
12 | std::tie(x, y, z) = triple; |