我想用 python 脚本从 python 文件中提取所有变量名,而不编辑 python 文件 [英] I want to extract all the variable names with a python script, from a python file, without editing the python file
问题描述
我正在制作一个文本编辑器,我可以在其中制作一个变量资源管理器.所以我想要做的是使用我的文本编辑器 python 脚本,我想从我的文本编辑器正在编辑的代码中获取所有变量名称.
如果还不清楚,那么我已经做了一个文本编辑器,如果用户正在使用我的编辑器编辑任何 python 文件,那么有一个函数可以从用户正在编辑的文件中提取所有变量.>
如果可能,获取变量的所有值
提前致谢.
我尝试使用 vars() 但它不适用于字符串,因为我只能使用字符串格式获取文本txt.get('1.0','end')但我不写.我还尝试在编辑文件(用户编辑的文件)中嵌入 vars()但我没有得到任何好的结果,或者你可以说没有结果.
而且我不知道如何使用dir"或如何在此脚本中使用它来获取变量名称.
所以我的预期结果是,当用户按下按钮时,函数启动并从用户当前正在编辑的 python 文件中获取所有变量及其值.像一个示例脚本
类示例():def example_define():a=1b=2c=3
并且函数返回像
{'a':1, 'b':2, 'c':3}
dir 和 __dict__
可用于根据类型了解对象内部的所有属性.例如,让我们考虑一下这段代码 code.py
.
类示例():def example_define(self):a=1b=2c=3d = 1定义你好():打印(世界")
在另一个文件 debug.py
上你可以试试这个.
导入代码print(code.__dict__) # 打印所有属性及其值的字典.print(dir(code)) # 打印所有属性的列表.
此外,如果您有一个对象 o
,您可以调用 dir(o)
来了解该对象的属性.如果支持,你可以试试o.__dict__
.
范围明智的可用性
class Example():def example_define(self):自我.a = 1自我.b = 2自我.c = 3o = 示例()print(dir(o)) # 不给出 a, b, co.example_define()print(dir(o)) # 给出 a, b, c
I am making a text editor in which I can make a variable explorer. So what I want to do is by using my python script which is text editor I want to get all the variable names from the code that my text editor is editing.
if not clear so again > I had made a text editor and if a user is editing any python file by using my editor so there is a function which will extract all the variables from the file which the user is editing.
If possible get all the values of variables
Thanks in advance.
I tried using vars() but it does not apply for strings because I can only get the text in string format using txt.get('1.0','end') but i it s not write. I also tried to embed vars() in the editing file(the file edited by the user) but I am not getting any good result or you can say no result.
and I don't know how to use "dir" or how can I use it in this script to get variables names.
So my expected result is when the user presses a button a function start and get all the variables and their values from the python file that the user is editing currently. like an example script
class example():
def example_define():
a=1
b=2
c=3
and the function return like
{'a':1, 'b':2, 'c':3}
dir and __dict__
can be useful to know all the attributes inside of an object based on the type. For Example,
Let's consider this code code.py
.
class example():
def example_define(self):
a=1
b=2
c=3
d = 1
def hello():
print("world")
On the other file debug.py
you can try this.
import code
print(code.__dict__) # prints a dictionary of all the attributes and their values.
print(dir(code)) # prints a list of all the attributes.
Also if you have an object o
you can call dir(o)
to know the attributes of the object. If it supports, you can try o.__dict__
.
Scope wise availability
class Example():
def example_define(self):
self.a = 1
self.b = 2
self.c = 3
o = Example()
print(dir(o)) # Doesn't give a, b, c
o.example_define()
print(dir(o)) # Gives a, b, c
这篇关于我想用 python 脚本从 python 文件中提取所有变量名,而不编辑 python 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!