And if there is no import? Or there is always an import?
If there is no import than either the name is defined in the module itself or itās the name of a builtin function, class or object:
You can get a list of those by doing:
dir(__builtins__)
If you see something like bar in code and you know it is not built in then it is either imported via
import bar. For this one you can start sleuthing source code by doingbar.__file__.from foo import bar. Here the module isfooso dofoo.__file__.from foo import baz as bar. Here the module isfooso dofoo.__file__.from foo import *. This is the nasty case. If you donāt seebarimported explicitly anywhere then you need to read the source code forfoo.__file__for eachimport *line that appears. This can get nasty if those modules also doimport *. This is whyimport *is a really bad pattern. It defeats code the ability to trace code, or at least makes it really hard. For example, if someone doesfrom math import *, I donāt know how to trace that the statementlog10was imported from themathmodule. But in this case you can dohelp(log10).
Obviously if you encounter something like a.bar you need to look for imports of a, not bar.
If bar is and object then you can check if it is built in by doing 'bar' in dir(__builtins__) like @hansgeunsmeyer suggests. If it is builtin I donāt really know the standard procedure to find the source code. For builtins I think it becomes likely that the source code behind an object is in c from cpython rather than in python so the rules change as far as I can tell.
But as others have pointed out, if the object is built in then there will be documentation on line for it.
The final case is that bar is not even an object but is rather an expression or part of an expression like pass, if, or lambda. All of these are just expressions in the language and to learn what these mean you have to learn the language when means you have to consume documentation or tutorials to learn about them. You might be able to get info by doing help('pass') but Iām not sure if that will work for all expressions.
If your goal is to learn how parts of code work then you will get the fastest results by googling and learning which webpages have the most relevant information.
If your goal is to learn how parts of code work WITHOUT using the internet then you will not get the fasters or best results, but you can try the methods above. But in this case, I have to ask why the constraint? Maybe your working in an air gapped system and canāt look up documentation while working all the time? I donāt have experience with this case but Iāve heard about it. If itās just a principled idea that all documentation should be available from the terminal then youāre going to be disappointed that most people using python just google things to figure out how the code works.
I generally agree with what @jagerber wrote, but⦠you donāt really need to google things - Google is no longer as usable as it was several years ago, since you will be overloaded with various courses, tutorials, and blogs, and what-notā¦
For a beginning student (actually for any Python dev) I would advise to always keep the Python documentation page open in your browser:
https://docs.python.org/3/index.html
In particular:
Library Reference
keep this under your pillow
There is an excellent search box and a glossary too.