Python:如何导入所有变量? [英] Python: How can I import all variables?

查看:363
本文介绍了Python:如何导入所有变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和编程的新手(一般最多几周)。

I'm new to Python and programming in general (a couple of weeks at most).

关于Python和使用模块,我意识到函数可以使用导入来自导入的 *

Concerning Python and using modules, I realise that functions can imported using from a import *.

所以不要输入

a.sayHi()
a.sayBye()

我可以说

sayHi()
sayBye()

我发现这很简单。现在,假设我有一堆变量我希望跨模块使用它们,我将它们全部定义在一个python模块中。我怎样才能使用上面提到的类似方法或同样简单的方法导入这些变量。我不想使用导入,然后需要在我的所有变量前加上 a。

which I find simplifies things a great deal. Now, say I have a bunch of variables that I want to use across modules and I have them all defined in one python module. How can I, using a similar method as mentioned above or an equally simple one, import these variables. I don't want to use import a and then be required to prefix all my variables with a..

理想情况如下:

a.py

name = "Michael"
age = 15

b.py

some_function

if name == "Michael":
    if age == 15:
        print("Simple!") 

输出:

Simple!


推荐答案

您自己给出了解决方案:来自导入* 将正常工作。 Python在这方面没有区分函数和变量。

You gave the solution yourself: from a import * will work just fine. Python does not differentiate between functions and variables in this respect.

>>> from a import *
>>> if name == "Michael" and age == 15:
...     print('Simple!')
...
Simple!

这篇关于Python:如何导入所有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆