python动态加载变量示例分享
众所周知,程序在启动后,各个程序文件都会被加载到内存中,这样如果程序文本再次变化,对当前程序的运行没有影响,这对程序是一种保护。
但是,对于像python这样解释执行的语言,我们有时候会用到“from 模块 import 变量名”这样的形式,如果这个变量直接被定义在文件当中,那么这些变量在程序开始时就会被定义、赋值,运行过程中值不变。如果打算在运行过程中对这个模块进行重写,那么更改后的变量值是无法被使用的。
对于这个问题,可以换一种思路,将这个模块中的变量定义在函数里,而函数是在程序运行的时候动态执行的,这样就能够获取到变量的最新值。下面是例子:
首先,不使用函数的情况:
#model1.py
p_hello = 'hello world!'
#test1.py
from model1 import p_hello
file = open('model1.py', 'w')
file.write("p_hello = '%s!'"%('hello you'))
file.close()
print p_hello
这样,执行test1.py的时候,出现的结果仍然是'hello world',而非‘hello you',说明变量已经加载到内存中,尽管该模块的文件在硬盘上已经被重写。
接下来,使用函数的情况:
#model1.py
def rule():
p_hello = 'hello world!'
return locals()
#test1.py
from model1 import rule
file = open('model1.py', 'w')
file.write('def rule():\n')
file.write(" p_hello = '%s!'\n"%('hello you'))
file.write(" return locals()\n")
file.close()
p_hello = rule()['p_hello']
print p_hello
这样,print出来的结果就是hello you 了,因为在运行的时候,先执行了一遍这个函数,再通过函数获取了这个变量,这样就会获得新值。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Python implements the dynamic loading and asynchronous request processing functions of headless browser collection applications. In web crawlers, sometimes it is necessary to collect page content that uses dynamic loading or asynchronous requests. Traditional crawler tools have certain limitations in processing such pages, and cannot accurately obtain the content generated by JavaScript on the page. Using a headless browser can solve this problem. This article will introduce how to use Python to implement a headless browser to collect page content using dynamic loading and asynchronous requests.

Handling dynamic loading and switching of components in Vue Vue is a popular JavaScript framework that provides a variety of flexible functions to handle the dynamic loading and switching of components. In this article, we will discuss some methods of handling dynamic loading and switching of components in Vue, and provide specific code examples. Dynamically loading components means dynamically loading components at runtime as needed. This improves the performance and loading speed of your application because relevant components are loaded only when needed. Vue provides async and awa

Exploring the Principle of Golang Hot Update: The Mystery of Dynamic Loading and Reloading Introduction: In the field of software development, programmers often hope to be able to modify and update code without restarting the application. Such requirements are of great significance to both development efficiency and system operation reliability. As a modern programming language, Golang provides developers with many convenient mechanisms to implement hot updates. This article will delve into the principles of Golang hot update, especially the mysteries of dynamic loading and reloading, and will combine it with specific code examples.

How to use Vue and Element-UI to create a table that dynamically loads data. In modern web development, data tables are one of the common interface components. Vue.js is a very popular front-end framework nowadays, and Element-UI is a set of component libraries developed based on Vue.js, which provides a rich set of UI components for us to use. This article will introduce how to use Vue and Element-UI to create a table that can dynamically load data, and give corresponding code examples. First, we need to install

Solve Vue error: Unable to correctly use VueRouter to dynamically load components based on routing parameters. In the process of using VueRouter for routing jumps, sometimes we need to dynamically load components based on routing parameters. However, in some cases, we may encounter a common error: unable to correctly use VueRouter to dynamically load components based on routing parameters. This article will describe how to resolve this error and provide code examples. First, we need to make it clear: VueRouter can pass

How to use reflection and dynamically load assemblies in C# Introduction: In C#, reflection (Reflection) is a powerful mechanism that allows us to obtain and operate the metadata of the program at runtime, including type information, member information, etc. Dynamically loading assemblies is a common application implemented through reflection, and is very useful in some specific scenarios. This article will introduce in detail how to use reflection and dynamically load assemblies in C#, and provide specific code examples. The basic concept of reflection Reflection is an important function in the C# language

phpSpider practical tips: How to deal with dynamic loading of web content? When crawling web page data, we often encounter the problem that dynamically loaded content cannot be obtained directly through the crawler. These dynamically loaded contents can be data obtained through AJAX requests, DOM elements rendered through JavaScript, etc. In order to solve this problem, this article will introduce some practical tips for dealing with dynamic loading problems of web pages when using phpSpider. 1. Use network debugging tools to find dynamically loaded URLs

Use the load() method of the System class in Java to dynamically load classes or resources. In Java development, sometimes we need to dynamically load classes or resources while the program is running to achieve some flexible functions. Java provides the load() method of the System class to achieve this requirement. This article will introduce the use of the load() method of the System class and provide corresponding code examples. First, let’s understand the definition of the load() method: publicstaticvo
