热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python:如何导入所有变量?-Python:HowcanIimportallvariables?

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

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 *.

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

So instead of typing

所以不要打字

a.sayHi()
a.sayBye()

I can say

我可以说

sayHi()
sayBye()

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..

我觉得它简化了很多东西。现在,假设我有一堆变量,我想在模块中使用它们,我将它们全部定义在一个python模块中。我怎样才能使用上面提到的类似方法或同样简单的方法导入这些变量。我不想使用import a然后被要求在我的所有变量前面添加一个..

The following situation would by ideal:

理想情况如下:

a.py

a.py

name = "Michael"
age = 15

b.py

b.py

some_function

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

Output:

输出:

Simple!

4 个解决方案

#1


56  

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

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

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

#2


26  

Just for some context, most linters will flag from module import * with a warning, because it's prone to namespace collisions that will cause headaches down the road.

只是对于某些上下文,大多数linters将从模块导入*标记警告,因为它容易发生命名空间冲突,这将导致头痛。

Nobody has noted yet that, as an alternative, you can use the

没有人注意到,作为替代方案,你可以使用

from a import name, age

form and then use name and age directly (without the a. prefix). The from [module] import [identifiers] form is more future proof because you can easily see when one import will be overriding another.

表格,然后直接使用名称和年龄(没有a。前缀)。 from [module] import [identifier]表单更具未来性,因为您可以轻松查看一个导入何时覆盖另一个导入。

Also note that "variables" aren't different from functions in Python in terms of how they're addressed -- every identifier like name or sayBye is pointing at some kind of object. The identifier name is pointing at a string object, sayBye is pointing at a function object, and age is pointing at an integer object. When you tell Python:

另请注意,“变量”与Python中的函数没有区别,因为每个标识符如name或sayBye都指向某种对象。标识符名称指向字符串对象,sayBye指向函数对象,age指向整数对象。当你告诉Python时:

from a import name, age

you're saying "take those objects pointed at by name and age within module a and point at them in the current scope with the same identifiers".

你说的是“在模块a中按名称和年龄指向那些对象,并使用相同的标识符在当前范围内指向它们”。

Similarly, if you want to point at them with different identifiers on import, you can use the

同样,如果要在导入时使用不同的标识符指向它们,则可以使用

from a import sayBye as bidFarewell

form. The same function object gets pointed at, except in the current scope the identifier pointing at it is bidFarewell whereas in module a the identifier pointing at it is sayBye.

形成。指向相同的函数对象,除了在当前作用域中,指向它的标识符是bidFarewell,而在模块a中,指向它的标识符是sayBye。

#3


12  

Like others have said,

像其他人说的那样,

from module import *

will also import the modules variables.

还将导入模块变量。

However, you need to understand that you are not importing variables, just references to objects. Assigning something else to the imported names in the importing module won't affect the other modules.

但是,您需要了解您不是导入变量,而是仅导入对象。在导入模块中为导入的名称分配其他内容不会影响其他模块。

Example: assume you have a module module.py containing the following code:

示例:假设您有一个模块module.py,其中包含以下代码:

a= 1
b= 2

Then you have two other modules, mod1.py and mod2.py which both do the following:

然后你有另外两个模块,mod1.py和mod2.py都执行以下操作:

from module import *

In each module, two names, a and b are created, pointing to the objects 1 and 2, respectively.

在每个模块中,创建两个名称a和b,分别指向对象1和2。

Now, if somewhere in mod1.py you assign something else to the global name a:

现在,如果mod1.py中的某个位置为全局名称指定了其他内容:

a= 3

the name a in module.py and the name a in mod2.py will still point to the object 1.

module.py中的名称a和mod2.py中的名称a仍将指向对象1。

So from module import * will work if you want read-only globals, but it won't work if you want read-write globals. If the latter, you're better off just importing import module and then either getting the value (module.a) or setting the value (module.a= …) prefixed by the module.

因此,如果您想要只读全局变量,那么从模块导入*将起作用,但如果您想要读写全局变量,它将无法工作。如果是后者,你最好只导入导入模块,然后获取值(module.a)或设置模块前缀的值(module.a = ...)。

#4


7  

You didn't say this directly, but I'm assuming you're having trouble with manipulating these global variables.

你没有直接说出来,但我假设你在操纵这些全局变量时遇到了麻烦。

If you manipulate global variables from inside a function, you must declare them global

如果从函数内部操作全局变量,则必须将它们声明为全局变量

a = 10
def x():
   global a
   a = 15

print a
x()
print a

If you don't do that, then a = 15 will just create a local variable and assign it 15, while the global a stays 10

如果你不这样做,那么a = 15将只创建一个局部变量并将其分配15,而全局a保持10


推荐阅读
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • Inno Setup区段之Components篇相关知识详解
    本文详细介绍了Inno Setup区段之Components篇相关的知识,包括Components和Types的使用方式以及各个参数的说明,希望对读者有一定的参考价值。内容涵盖了ComponentsName、Description、Types、ExtraDiskSpaceRequired、ExtraDiskSpaceRequiredFlags等多个关键词,帮助读者更好地理解和应用Inno Setup区段之Components篇的知识。 ... [详细]
  • 开发笔记:Python之路第一篇:初识Python
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Python之路第一篇:初识Python相关的知识,希望对你有一定的参考价值。Python简介& ... [详细]
  • 开发笔记:python协程的理解
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了python协程的理解相关的知识,希望对你有一定的参考价值。一、介绍什么是并发?并发的本质就是 ... [详细]
  • Python语言简介、环境搭建、PyCharm的安装和配置
    一、Python语言简介1.1Python语言的基本概念官方对Python语言的介绍如下:Python是一款易于学习且功能强大的编程语言。它具有高效率的数据结构,能够简单又有效地实 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • Oracle优化新常态的五大禁止及其性能隐患
    本文介绍了Oracle优化新常态中的五大禁止措施,包括禁止外键、禁止视图、禁止触发器、禁止存储过程和禁止JOB,并分析了这些禁止措施可能带来的性能隐患。文章还讨论了这些禁止措施在C/S架构和B/S架构中的不同应用情况,并提出了解决方案。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
author-avatar
persisit110
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有