SoFunction
Updated on 2024-07-15

Learning Python through C++

Would I just say that C++ has been "copying" Python in recent years? I'll just say that I'm learning Python in C++.

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

literal amount

Python has supported binary as a literal since version 2.61, and more recently C++14 has matured to support it2.

Copy Code The code is as follows.

static const int primes = 0b10100000100010100010100010101100;

Not to mention that Python has had the concept of raw string literals since 1.5,3 and we're not too far behind in C++, which has a similar approach in C++11:

Copy Code The code is as follows.

const char* path = r"C:\Python27\Doc";
Range Loop

Python is a very comfortable place to write for loops:

Copy Code The code is as follows.

for x in mylist:
    print(x);

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

Copy Code The code is as follows.

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

Automatic type derivation

Is there really a concept of types in Python? (Laughs.

Copy Code The code is as follows.

x = "Hello World"
print(x)

C++11 has also learned this trick, except that it retains the old lady's shroud (auto).

Copy Code The code is as follows.

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

tuple

Python's long envied tuples have been around since the beginning of Python.

Copy Code The code is as follows.

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

Well, I'll use C++11 to do the same thing:

Copy Code The code is as follows.

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

Someone said, "Python is great, you can reverse parse it into a variable.

Copy Code The code is as follows.

x, y, z = triple

Heck, can't C++?

Copy Code The code is as follows.

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

Lists

In Python, Lists are a built-in type4 and it's incredibly easy to create a list:

Copy Code The code is as follows.

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

In the past, we could say, "What's the big deal, std::vector can pretty much do the same thing". But Python got serious: can you initialize like that? This was heard by Bjarne Stroustrup's dad, who, to his shame, responded with an initializer_list in C++115.

Copy Code The code is as follows.

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 anything6.

Copy Code The code is as follows.

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

Che, C++ already has a map type, and now there's a hash table, unordered_map, which is more like.

Copy Code The code is as follows.

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

Lambda Expressions

Python has come up with the big guns, Lambda expressions, which have been around since 1994:

Copy Code The code is as follows.

(key = lambda x: abs(x))

C++11 started a poor imitation:

std::sort((), (), [](int x, int y){ return std::abs(x) < std::abs(y); });
And Python stepped up its game in 2001, introducing the Nested Scopes technique7 :

Copy Code The code is as follows.

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

Not to be outdone, C++11 came up with the capture-list concept8 .

Copy Code The code is as follows.

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

Built-in algorithms

Python has a number of powerful algorithmic functions built in, such as filter.

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

Copy Code The code is as follows.

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

Functions like this are common in <algorithm> and all echo some kind of functionality in Python: transform, any_of, all_of, min, max.

Variable parameters

Python has supported variable parameters since the beginning. You can define a function with a variable number of parameters, of an indeterminate number, and of a different type.

Copy Code The code is as follows.

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

In C++11, initializer_list supports a variable number of parameters of the same type (C++ Primer 5th 6.2.6).

Copy Code The code is as follows.

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

foo({4, 5, 6});

Do you find that learning Python in C++ is a good way to learn Python? As you can see from the answer to this question, @Milo Yip is one of us.

proceed with

Feeling good? Want to make a splash? Check out this repo. It has more ways to learn Python in C++.