r/programmingmemes • • 6d ago

C++ VS python

Post image
754 Upvotes

37 comments sorted by

63

u/DatsLotus 6d ago

Uh... is the analogy C++: make it yourself vs Python: take a slice of already existing code?

62

u/David_Dantas 6d ago • Bravo

In Python you:
import cake

In C++

#include <iostream>
#include <string>

class Cake {
public:
    std::string flavor;
    int layers;
    bool hasFrosting;

    Cake(std::string f, int l, bool frosting) 
        : flavor(f), layers(l), hasFrosting(frosting) {}

    void serve() {
        std::cout << "Serving a " << layers << "-layer " << flavor << " cake!" << std::endl;
    }
};

int main() {
    Cake myCake("Chocolate", 3, true);
    myCake.serve();
    return 0;
}

33

u/oleivas 5d ago

Actually....your application failed with:

Traceback (most recent call last):
  File "/home/user/projects/bakery/bakery.py", line 3, in <module>
    from cake import Cake
  File "/home/user/projects/bakery/cake/__init__.py", line 1, in <module>
    from .cake import Cake
  File "/home/user/projects/bakery/cake/cake.py", line 7, in <module>
    import flour
ModuleNotFoundError: No module named 'flour'

flour didn't got installed because you are running python 3.12.4, which only supports flour versions 3.2.1 to 4.0.2. However, latest version of cake depends on flour 2.1.3 to 2.5.63

13

u/DatsLotus 6d ago

So, that's a yes, then.

9

u/GamingGo2022 6d ago

well that's not a very fair comparison now is it

8

u/SpaceCadet87 6d ago
#include <cake>

int main() {
    Cake myCake("Chocolate", 3, true);
    myCake.serve();
    return 0;
}

1

u/Total-Box-5169 5d ago
#include <cake>

int main() {
    Cake{"Chocolate", 3, true}.serve();
}

2

u/SpaceCadet87 5d ago

Oh, I didn't realise we were doing code golf.

M{Cake{"Chocolate",3,1}.serve();}

g++ -D'M=int main()' -include cake x.cpp

1

u/DatsLotus 4d ago

Damn, you've bested me. I could only follow up until the previous one.

2

u/SpaceCadet87 4d ago

Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.

The rest can all fit on a single line.

1

u/DatsLotus 4d ago

The more I learn, the more I discover I have more to learn.

2

u/Reasonable-Class3728 5d ago

I remember 30 years ago there was similar joke about Delphi.

If you need to write a program that puts a tiger into a cage,
In C, you write a structure for the tiger, a structure for the cage and a function that puts the tiger into the cage.
In Java, you write a fabric that produces the tiger with the cage around it.
In Delphi, you import a module that puts the tiger into the cage.

Probably there was some other languages in that joke, but I don't remember it.

37

u/JohnClark13 6d ago

Assembly - "If you want to make the cake from scratch you must first invent the universe"

1

u/Zuruumi 5d ago

That's when you start by planting wheat, sugar cane, buying a calf, etc.

7

u/overclockedslinky 6d ago

pretty accurate: python just uses things made by c++

better analogy would've been c

1

u/My92thAccount 2d ago

That's the whole point of Python anyway.
Just let the C++ guys do all the crazy optimizations.
And then just leverage the incredible Python syntax to code whatever sorcery you want.

Your C++ for loop may be orders of magnitude faster than your Python one,
but boi aren't you anyday get any faster than Numpy or Pytorch, and you ain't gonna recode that from scratch.

6

u/itemluminouswadison 6d ago

Why are my eggs rotten?

Default args are instantated at time of definition, not invocation.

5

u/nix206 6d ago

The cake is a lie.

3

u/FLMKane 5d ago

Cake != True

8

u/teymuur 6d ago

4

u/sub_doesnt_exist_bot 6d ago

The subreddit r/firstweekcodermemes does not exist.

Did you mean?:

Consider creating a new subreddit r/firstweekcodermemes.


πŸ€– πšƒπ™·π™Έπš‚ π™²π™Ύπ™Όπ™Όπ™΄π™½πšƒ πš†π™°πš‚ πš†πšπ™Έπšƒπšƒπ™΄π™½ π™±πšˆ 𝙰 π™±π™Ύπšƒ. 𝙱𝙴𝙴𝙿 𝙱𝙾𝙾𝙿 πŸ€–

GitHub | Rank

3

u/teymuur 6d ago

good bot thats what i meant

1

u/No_Belt_9829 5d ago

Good bot

2

u/MohakAoki 5d ago

c++: you can do whatever you want and change anything you want python: use this complete package and dont argue.

c++: you know fishing python: you have a fish

although python is written in C

1

u/DragonfruitGrand5683 6d ago

C - Object egg is not defined

1

u/Lines25 5d ago

And C is the same as C++ but don't have a bowl nor a mixer

1

u/kinchkun 5d ago

Both are just awful languages.

1

u/BarfingOnMyFace 4d ago

You bake c++ into python...?

1

u/sdjopjfasdfoisajnva 6d ago

give the batter a gun to shoot your leg