我正在学习Python,我仍然是初学者,虽然我已经研究了大约一年了.我正在尝试编写一个在主模块中调用的函数模块.被调用模块中的每个函数都需要运行数学模块.我想知道是否有办法在不调用被调用模块内的数学模块的情况下执行此操作.这是我有的:
main.py:
from math import *
import module1
def wow():
print pi
wow()
module1.cool()
Run Code Online (Sandbox Code Playgroud)
module1.py:
def cool():
print pi
Run Code Online (Sandbox Code Playgroud)
跑步时main.py我得到:
3.14159265359
Traceback (most recent call last):
File "Z:\Python\main.py", line 10, in <module>
module1.cool()
File "Z:\Python\module1.py", line 3, in cool
print pi
NameError: global name 'pi' is not defined
Run Code Online (Sandbox Code Playgroud)
我很难理解的是为什么我在运行时遇到名称错误main.py.我知道变量pi在导入时变为主模块的全局变量,因为wow可以访问它.我也知道cool在导入时对主模块变得全局,因为我可以打印module1.cool并获取<function cool at 0x02B11AF0>.因此,因为cool它位于主模块的全局命名空间内,所以程序不应该首先查看cool变量的函数内部pi,然后当它找不到它时,查看main模块内部的变量pi并在那里 …
(有许多类似的和更通用的问题,在阅读完它们之后尝试过它们的解决方案,无法让它们工作所以在这里要求我看到的更具特定情况的版本)
我认为我真的很想念 - 由于我更多的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) 按照这里的其他帖子,我有一个函数,根据其名称打印出有关变量的信息.我想把它移到一个模块中.
#python 2.7
import numpy as np
def oshape(name):
#output the name, type and shape/length of the input variable(s)
#for array or list
x=globals()[name]
if type(x) is np.array or type(x) is np.ndarray:
print('{:20} {:25} {}'.format(name, repr(type(x)), x.shape))
elif type(x) is list:
print('{:20} {:25} {}'.format(name, repr(type(x)), len(x)))
else:
print('{:20} {:25} X'.format(name, type(t)))
a=np.array([1,2,3])
b=[4,5,6]
oshape('a')
oshape('b')
Run Code Online (Sandbox Code Playgroud)
输出:
a <type 'numpy.ndarray'> (3,)
b <type 'list'> 3
Run Code Online (Sandbox Code Playgroud)
我想把这个函数oshape()放到一个模块中,以便它可以重用.但是,放置在模块中不允许从主模块访问全局变量.我尝试过'import __main__'之类的东西,甚至存储函数globals()并将其传递给子模块.问题是globals()是一个函数,它专门返回调用它的模块的全局变量,而不是每个模块的不同函数.
import numpy as np
import olib
a=np.array([1,2,3])
b=[4,5,6]
olib.oshape('a')
olib.oshape('b')
Run Code Online (Sandbox Code Playgroud)
给我:
KeyError: 'a'
Run Code Online (Sandbox Code Playgroud)
额外信息:目标是减少冗余打字.稍微修改一下(我把它拿出去使问题更简单),oshape可以报告变量列表,所以我可以使用它: …
我有一个在Postgres和Mysql上运行的应用程序.每个程序检查以确定数据库,然后将postgres_db作为db_util或mysql_dt导入为db_util.当主引用db_util中的代码时,一切正常,但如果导入了类,则不会定义对db_util的引用.
我创建了以下类和main来测试问题,并发现了另一个有趣的副作用.B&C类在不同的导入情况下引用ClassA.B&C是相同的,除了B是主要的,C是进口的.
ClassX.py
class ClassA(object):
def print_a(self):
print "this is class a"
class ClassC(object):
def ref_a(self):
print 'from C ref a ==>',
xa=ClassA()
xa.print_a()
def ref_ca(self):
print 'from C ref ca ==>',
xa=ca()
xa.print_a()
Run Code Online (Sandbox Code Playgroud)
test_scope.py
from classes.ClassX import ClassA
from classes.ClassX import ClassA as ca
from classes.ClassX import ClassC as cb
class ClassB(object):
def ref_a(self):
print 'from B ref a ==>',
xa=ClassA()
xa.print_a()
def ref_ca(self):
print 'from B ref ca ==>',
xa=ca()
xa.print_a()
print 'globals:',dir()
print 'modules','ca:',ca,'cb:',cb,'CA:',ClassA
print ''
print …Run Code Online (Sandbox Code Playgroud) 我是python的新手,现在用它制作一些程序.
我有两个文件,它们是trans.py和main.py.
在trans.py,
from math import *
from numpy import *
def matrix_T(d):
global mat_A, mat_B, mat_C, mat_D
temp_A, temp_B, temp_C, temp_D = mat_A, mat_B, mat_C, mat_D
mat_A = 1.0*temp_A + d*temp_C
mat_B = 1.0*temp_B + d*temp_D
mat_C = 1.0*temp_C
mat_D = 1.0*temp_D
Run Code Online (Sandbox Code Playgroud)
在main.py,
from trans import *
global mat_A, mat_B, mat_C, mat_D
mat_A = 1.0
mat_B = 0.0
mat_C = 0.0
mat_D = 1.0
print(mat_A, mat_B, mat_C, mat_D)
matrix_T(0.0)
print(mat_A, mat_B, …Run Code Online (Sandbox Code Playgroud) python ×5
import ×2
module ×2
global ×1
local ×1
nameerror ×1
namespaces ×1
python-2.7 ×1
scope ×1