Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include in C++ vs import in python

Tags:

c++

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?

like image 692
user97662 Avatar asked Oct 16 '25 09:10

user97662


1 Answers

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

like image 148
Alex Martelli Avatar answered Oct 19 '25 01:10

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!