Learn Python through C++

  • 2020-04-02 14:32:04
  • OfStack

Will I just say that C++ has started to "copy" Python in recent years? All I can say is, I'm learning Python in C++.

Don't believe it? Come and learn from me?

literal

Python has supported binary as a literal 1 since version 2.6, and C++14 recently matured to support this 2:


static const int primes = 0b10100000100010100010100010101100;

Not to mention that Python had the concept of raw string literals in 1.5. 3, we are not too late in C++.


const char* path = r"C:Python27Doc";
Range Loop

Python writing a for loop is a very nice thing to do:


for x in mylist:
    print(x);

As you all know, I was finally able to do the same thing in C++11:


for (int x : mylist)
    std::cout << x;

Automatic type derivation

Is there really a concept of type in Python? (laughs


x = "Hello World"
print(x)

C++11 also learned this trick, but kept the old lady's foot-binding cloth (auto).


auto x = "Hello World";
std::cout << x;

tuples

Python has had an enviable tuple since the beginning.


triple = (5, "Hello", True)
print(triple[0])

Okay, I'll do it in C++11:


auto triple = std::make_tuple(5, "hello", true);
std::cout << std::get<0>(triple);

Some people said that Python method is good, but also can be reversed into variables


x, y, z = triple

Well, can't C++ work?


std::tie(x, y, z) = triple;

Lists

In Python, Lists are built into type 4, so creating a list is incredibly easy:


mylist = [1, 2, 3, 4]
mylist.append(5);

In the old days we could say, what's this? STD ::vector is pretty much capable of it. But the Python pink is serious. Can you initialize as above? Dad Bjarne Stroustrup heard this, felt ashamed, and made an initializer_list in C++ 11 to respond 5.


auto mylist = std::vector<int>{1,2,3,4};
mylist.push_back(5);

But then again, creating a Dictionary in Python is as easy as anything.


myDict = {5: "foo", 6: "bar"}
print(myDict[5])

Cut, C++ itself has the map type, now there is a hash table unordered_map, more like:


auto myDict = std::unordered_map<int, const char*>{ { 5, "foo" }, { 6, "bar" } };
std::cout << myDict[5];

Lambda expressions

Python is a big thing, and Lambda expressions have been around since 1994:


mylist.sort(key = lambda x: abs(x))

C++11 began its parody:

STD ::sort(mylist.begin(), mylist.end(), [](int x, int y){return STD ::abs(x) < STD: : abs (y); });
Python added a bit of muscle in 2001, introducing the technology of Nested Scopes 7:


def adder(amount):
return lambda x: x + amount
...
print(adder(5)(5))

Not to be outdone, C++11 completes the capture-list concept 8.


auto adder(int amount) {
    return [=](int x){ return x + amount; };
}
...
std::cout << adder(5)(5);

Built-in algorithm

Python has many powerful built-in algorithm functions, such as filter:

Result = filter(mylist, lambda x: x) > = 0)
C++11 can do the same thing with STD ::copy_if:


auto result = std::vector<int>{};
std::copy_if(mylist.begin(), mylist.end(), std::back_inserter(result), [](int x){ return x >= 0; });

So this function right here < algorithm > Transform, any_of, all_of, min, Max.

Variable parameter

Python has supported mutable arguments since the beginning. You can define a function of variable parameters, the number can be uncertain, the type can be different.


def foo(*args):
    for x in args:
        print(x);
foo(5, "hello", True)

Initializer_list in C++11 can support a variable number of arguments of the same type (C++ Primer 5th 6.2.6).


void foo(std::initializer_list<int> il) {
    for (auto x : il)
        std::cout << x;
} foo({4, 5, 6});

Do you see here that learning Python in C++ is a great way to do it? The answer to this question suggests that @milo Yip is a fellow artist.

Continue to

Feel good? Want to make it big? Check out this repo. There are more ways to learn Python in C++.


Related articles: