How to open the code of a Python module?

Hey everyone! I am currently working on the “Basta Fazoolin’” exercise in the Python 3 tutorial.

Currently at step 8, and before moving further I wanted to find out if there was a way to list all instances of a specific class and (for example) print their repr value.

Surely enough, there is one. It can be done in 4 lines of codes thanks to the gc module.

So, I tried it, works beautifully. So far so good:

import gc
class Menu:
  def __init__(self, name, items, start_time, end_time):
    self.name = name
    self.items = items
    self.start_time = start_time
    self.end_time = end_time
    pass
  def __repr__(self):
    return '{Menu} - Available from {btime}:00 to {etime}.00'.format(Menu = self.name, btime = self.start_time, etime = self.end_time)
    
  pass

brunch = Menu('Brunch', {'pancakes': 7.50, 'waffles': 9.00, 'burger': 11.00, 'home fries': 4.50, 'coffee': 1.50, 'espresso': 3.00, 'tea': 1.00, 'mimosa': 10.50, 'orange juice': 3.50}, 11, 16)

early_bird = Menu('Early-bird', {'salumeria plate': 8.00, 'salad and breadsticks (serves 2, no refills)': 14.00, 'pizza with quattro formaggi': 9.00, 'duck ragu': 17.50, 'mushroom ravioli (vegan)': 13.50, 'coffee': 1.50, 'espresso': 3.00}, 15, 18)

dinner = Menu('Dinner', {'crostini with eggplant caponata': 13.00, 'ceaser salad': 16.00, 'pizza with quattro formaggi': 11.00, 'duck ragu': 19.50, 'mushroom ravioli (vegan)': 13.50, 'coffee': 2.00, 'espresso': 3.00}, 17, 11)

kids = Menu('Kids', {'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00}, 11, 21)

for x in gc.get_objects():
    if isinstance(x, Menu):
        print(x)

But then I tried to play a little with this gc module. I tried to add each iteration into a list before printing that list, by replacing the last 3 lines of the previous code by:

for x in gc.get_objects():
    lst1 = []
    if isinstance(x, Menu):
        lst1.append(x.name)
        pass
    print(lst1)

And some interesting problems started to appear. I believe it has to do with this gc module and how it is structured.

Which, finally, brings me to my question : I’m not quite clear at this point as to what those modules that we can import are. I know how to import them to my code, I know how to find documentation about them online, but I don’t know where on my computer they reside and whether I can see their code.

Could you please help me figure this out? I don’t even know what to Google anymore. :smile:

PS: I also just realised that I never had any of my code read by someone else before. So if my code, or anything I wrote in this message betrays a profound misunderstanding of some key concepts, please let me know :slight_smile:

Thanks!

Your code file is a module, a file with text in it. That’s the common case.
Some modules provided by python itself might be implemented entirely in C, or maybe it’s a python module with imports from C, you’d then need to look for the source code because that would be compiled into a binary format that isn’t human readable.

modules have a __file__ attribute that says where it’s installed

>>> import numpy
>>> numpy.__file__
'/usr/lib/python3.8/site-packages/numpy/__init__.py'
>>> import math
>>> math.__file__
'/usr/lib/python3.8/lib-dynload/math.cpython-38-x86_64-linux-gnu.so'

you’d find math’s source here (if you care about it matching your version then you’d look at it at the commit that your version was compiled from)
https://github.com/python/cpython/blob/master/Modules/mathmodule.c


for tracking class instances you’d do the same as for any other bunch of things you wanted to keep hold of. put them in a list or dict or whatever container suits you

for _ in lots_of_stuff:
    lst1 = []
    print(lst1)

If you mean writing lots of empty lists, that’s exactly what your code says to do

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.