我正在尝试理解Python的变量范围方法.在这个例子中,为什么f()能够改变x内部感知main()的价值,而不是价值n?
def f(n, x):
n = 2
x.append(4)
print('In f():', n, x)
def main():
n = 1
x = [0,1,2,3]
print('Before:', n, x)
f(n, x)
print('After: ', n, x)
main()
Run Code Online (Sandbox Code Playgroud)
输出:
Before: 1 [0, 1, 2, 3]
In f(): 2 [0, 1, 2, 3, 4]
After: 1 [0, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud) 使用Python文档中的结构:
sound/
__init__.py
effects/
__init__.py
echo.py
surround.py
reverse.py
Run Code Online (Sandbox Code Playgroud)
说我想要import sound.effects并获得可用效果的列表.我可以在声明模块级变量做到这一点sound.effects,然后当每个.py文件导入追加到它.所以声音/效果/ __ init__.py可能是这样的:
effectList = []
import echo
import surround # Could write code to import *.py instead
...
Run Code Online (Sandbox Code Playgroud)
从我的主代码我现在可以访问sound.effects.effectList以获取效果列表,但如何effectList从echo.py中访问以进行实际追加?我被困在试图访问变量:
# None of these work :-(
# from . import self
# from .. import effects
# import sound.effects
sound.effect.effectList.append({'name': 'echo'})
Run Code Online (Sandbox Code Playgroud) (有许多类似的和更通用的问题,在阅读完它们之后尝试过它们的解决方案,无法让它们工作所以在这里要求我看到的更具特定情况的版本)
我认为我真的很想念 - 由于我更多的C#/ C++背景,Python如何处理OOP.所以这就是我现在正在努力做的事情.
我从两个模块开始设置项目的其余部分,部分是作为一个完整性检查和概念验证.一个模块将内容记录到文件中,同时还存储来自多个模块的数据(最终将它们打包并根据请求转储它们)在PyCharm中完成所有这些并提及它建议的错误警告,并使用Python 2.7
Module 1:
src\helpers\logHelpers.py
class LogHelpers:
class log:
def classEnter():
#doing stuff
def __init__(self):
self.myLog = LogHelpers.log() #forgot to mention this was here initially
[..] various logging functions and variables to summarize what's happening
__builtin__.mylogger = LogHelpers
Module 2:
src\ULTs\myULTs.py
mylogger.myLog.classEnter()
Run Code Online (Sandbox Code Playgroud)
(模块和根src \中都有一个空的init .py文件)
因此,根据这里非常棒的响应(Python - 导入模块中的全局变量的可见性),这应该是有效的,但是'mylogger'成为'未解析的引用'
这是一种方法.我也尝试过更直接的全局变量(Python:如何制作跨模块变量?)
Module 1:
src\helpers\logHelpers.py
class LogHelpers:
class log:
def classEnter(self):
#doing stuff
def __init__(self):
self.myLog = LogHelpers.log() #forgot to mention …Run Code Online (Sandbox Code Playgroud) 我提出了这个想法如何制作跨模块变量?在 python3 中运行。并且懒得使用变量__builtins__而不是模块builtins。这应该没有区别,因为:
# file spam.py:
import builtins
print (builtins is __builtins__)
print (id(builtins))
print (id(__builtins__))
Run Code Online (Sandbox Code Playgroud)
这是它变得有趣的时候:builtins不是__builtins__在导入时:
$ python3 spam.py
True
140598001743336
140598001743336
$ python3 -c 'import spam'
False
139755426543080
139755426520904
Run Code Online (Sandbox Code Playgroud)
有谁知道会发生什么?
(给定页面上的评论提到“__builtins__是 CPython 的特性,你真的不应该使用它”,但我很好奇......)
我调查了python中全局变量的范围仅限于模块.但我需要范围在不同模块之间是全局的.有这样的事吗?我玩了__builtin__但没有运气.
提前致谢!
例如,我希望能够从导入的类中调用全局函数
在文件PetStore.py中
class AnimalSound(object):
def __init__(self):
if 'makenoise' in globals():
self.makenoise = globals()['makenoise']
else:
self.makenoise = lambda: 'meow'
def __str__(self):
return self.makenoise()
Run Code Online (Sandbox Code Playgroud)
然后当我在Python Interpreter中测试时
>>> def makenoise():
... return 'bark'
...
>>> from PetStore import AnimalSound
>>> sound = AnimalSound()
>>> sound.makenoise()
'meow'
Run Code Online (Sandbox Code Playgroud)
我得到'喵'而不是'吠'.我已经尝试过使用python-how-to-make-a-cross-module-variable中提供的解决方案而没有运气.