0

How could I see the amount of attributes and the list of variables within file1 within file2. Is there a function or such that would allow me to see such a thing?

file1.py contents

value = 3
Random_numb = 6
lev = 8

file2.py contents

import file1

file2s Expected output:

List of variables:
value
Random_numb 
lev 
ege Selcuk
  • 227
  • 2
  • 9
  • if you want to see which variables are declared use `dir("file1.py")`, if you want the values as such, use `file1.value` – Bijay Regmi Mar 25 '21 at 20:30

1 Answers1

0
for name_local in dir(file1):
    if not name_local.startswith('__'):
        print(f'{name_local}: {getattr(file1, name_local)}')

dir(file1) returns all attributes of that file but also some special attributes (like __file__) which are of no interest for you. So: Do only print the attributes that do not start with '__'

Durtal
  • 1,063
  • 3
  • 11
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Mar 26 '21 at 09:41