How to find an object?

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 doing bar.__file__.
  • from foo import bar. Here the module is foo so do foo.__file__.
  • from foo import baz as bar. Here the module is foo so do foo.__file__.
  • from foo import *. This is the nasty case. If you don’t see bar imported explicitly anywhere then you need to read the source code for foo.__file__ for each import * line that appears. This can get nasty if those modules also do import *. This is why import * is a really bad pattern. It defeats code the ability to trace code, or at least makes it really hard. For example, if someone does from math import *, I don’t know how to trace that the statement log10 was imported from the math module. But in this case you can do help(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.

3 Likes