user97662
user97662

Reputation: 960

include in C++ vs import in python

is "import" in python equivalent to "include" in c++?

Can I consider namespaces from c++ the same way I do with python module names?

Upvotes: 4

Views: 4853

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881805

#include in C and C++ is a textual include. import in Python is very different -- no textual inclusion at all!

Rather, Python's import lets you access names exported by a self-contained, separately implemented module. Some #includes in C or C++ may serve similar roles -- provide access to publicly accessible names from elsewhere -- but they could also be doing so many other very different things, you can't easily tell.

For example it's normal for a .cc source file to #include the corresponding .h header file to make sure it's implementing precisely what that header file makes available elsewhere -- there's no equivalent of that in Python (or Java or AFAIK most ohter modern languages).

#include could also be about making macros available... and Python very deliberately chooses to have no macros, so, no equivalence!-)

All in all, I think the analogy is likely to be more confusing than helpful.

Upvotes: 7

Related Questions