W
W
weranda2021-08-02 17:09:00
Python
weranda, 2021-08-02 17:09:00

How to determine file attributes - not magic methods?

Greetings.
There is a file. The file has attributes/variables:

# file.py
a = 10
b = 20

I import this file into the file handler: Now somehow we need to find out what attributes / variables the file has, and there are two of them - and . Various options come to mind, including:
import file as file_data
ab
vars = []
for item in file_data.__dir__():
    if '__' not in item:
        vars.append(item)

How to properly get attributes/namespace (visible) of a file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2021-08-02
@lob4Noff

What prevents you from directly passing the values ​​of these variables to the file handler?
from file import a, b

V
Vindicar, 2021-08-02
@Vindicar

vars(file), then weed out all keys. starting with __.
But the method is so-so, you can execute any code in this file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question