我不會發布代碼,我只會引用相關部分,因爲它有點長。Python模塊導入問題
所以,我有一個主要的.py文件,我定義了一些類。當我運行該文件並使用類中定義的過程時,一切正常。現在,我想創建一個主要的.py文件,它將導入帶有所有定義的文件,即「from some_file import *」。現在,當我執行這個文件時,一個過程不起作用,並給出NameError(全局名稱some_attribute未定義)。現在,這個過程在其中一個類中定義,並使用另一個類中創建的屬性。出於某種原因,這個參考不起作用,我不知道我做錯了什麼。在這裏,我會從「主」文件給出代碼:
from fem_obj import *
nd = Node()
nd.nodegen(1, 1, 0, 0, 4, 0, 4)
el = Element()
el.elgen(1, 1, 1, 2, 1, 1, 3)
fem_obj是我的課節點和單元文件。 nd和el當然是他們的實例。 nodegen和elgen是方法。現在,其他文件中的elgen方法引用了在調用nodegen過程之後創建的屬性。在那裏我得到一個名稱錯誤。即,elgen方法使用nd.nodes,它是由nodegen方法創建的列表。
我希望這已經足夠清楚了,如果需要的話我可以發佈其他文件的代碼。預先感謝您的幫助。
OK,這裏的代碼fem_obj:
from math import hypot, sqrt
from Tkinter import *
class Node:
def __init__(self):
self.nodes = []
self.nodenum = 0
def nodegen(self, slabel, dlabel, sx, sy, dx, dy, num):
for i in range(1, num + 1):
label = slabel + (i - 1)*dlabel
if self.nodecheck(label) == True:
return "Node %s already exists!" %label
self.nodes = self.nodes + [[label, (sx + (i - 1)*dx, sy + (i - 1)*dy), (0, 0, 0)]]
self.nodenum += 1
def nodegenf(self, slabel, dlabel, sx, dx, num):
f = lambda x: sqrt(1 - x**2)
for i in range(1, num + 1):
label = slabel + (i - 1)*dlabel
if self.nodecheck(label) == True:
return "Node %s already exists!" %label
self.nodes = self.nodes + [[label, (sx + (i - 1)*dx, f(sx + (i - 1)*dx)), (0, 0, 0)]]
self.nodenum += 1
def nodeadd(self, label, sx, sy):
if self.nodecheck(label) == True:
return "Node %s already exists!" %label
self.nodes = self.nodes + [[label, (sx, sy), (0, 0, 0)]]
self.nodenum += 1
def nodedel(self, label):
if self.nodecheck(label) == False:
return "Node %s does not exist!" %label
for i in el.elements:
if label in i[1]:
return "Node %s attached to element %s!" %(label, i[0])
for i in self.nodes:
if label == i[0]:
self.nodes.remove(i)
self.nodenum -= 1
def nodecheck(self, label):
for i in self.nodes:
if label == i[0]:
return True
return False
def noderes(self, label, u, v, r):
if self.nodecheck(label) == False:
return "Node %s does not exist!" %label
for i in self.nodes:
if label == i[0]:
i[2] = (u, v, r)
def nodelist(self):
if self.nodes == []:
return "No nodes defined!"
print "\n"
print "NUMBER OF NODES: ", self.nodenum
print "\n"
print "NODE COORDINATES RESTRAINTS"
print "---- ----------- ----------"
print "\n"
for i in self.nodes:
print i[0], " ", i[1], " ", i[2]
class Element:
def __init__(self):
self.elements = []
self.elnum = 0
def elgen(self, slabel, dlabel, snl, enl, sndl, endl, num):
for i in range(1, num + 1):
label = slabel + (i - 1)*dlabel
if self.elcheck(label) == True:
return "Element %s already exists!" %label
if self.elements != []:
for j in self.elements:
if j[1] == (snl + (i - 1)*sndl, enl + (i - 1)*endl):
return "Element %s endnodes already taken!" %label
hlplst = []
for j in nd.nodes: # nd reference
hlplst = hlplst + [j[0]]
if snl + (i - 1)*sndl not in hlplst:
return "Node %s does not exist" %(snl + (i - 1)*sndl)
if enl + (i - 1)*endl not in hlplst:
return "Node %s does not exist" %(enl + (i - 1)*endl)
self.elements = self.elements + [[label, (snl + (i - 1)*sndl, enl + (i - 1)*endl)]]
self.elnum +=1
self.ellen()
def elcheck(self, label):
for i in self.elements:
if label == i[0]:
return True
return False
def eladd(self, label, snl, enl):
if self.elcheck(label) == True:
return "Element %s already exists!" %label
if self.elements != []:
for j in self.elements:
if j[1] == (snl, enl):
return "Endnodes already taken by element %s!" %j[0]
hlplst = []
for j in nd.nodes: # nd reference
hlplst = hlplst + [j[0]]
if snl not in hlplst:
return "Node %s does not exist" %snl
if enl not in hlplst:
return "Node %s does not exist" %enl
self.elements = self.elements + [[label, (snl, enl)]]
self.elnum +=1
self.ellen()
def eldel(self, label):
if self.elcheck(label) == False:
return "Element %s does not exist!" %label
for i in self.elements:
if label == i[0]:
self.elements.remove(i)
self.elnum -=1
def ellen(self):
if self.elements == []:
return "No elements defined!"
for i in self.elements:
if len(i) == 2:
x1 = y1 = x2 = y2 = 0
for j in nd.nodes:
if i[1][0] == j[0]:
x1 = j[1][0]
y1 = j[1][1]
elif i[1][1] == j[0]:
x2 = j[1][0]
y2 = j[1][1]
i.append(round(hypot(x1 - x2, y1 - y2), 4))
else:
continue
def ellist(self):
if self.elements == []:
return "No elements defined!"
print "\n"
print "NUMBER OF ELEMENTS: ", self.elnum
print "\n"
print "ELEMENT START NODE END NODE LENGTH SECTION"
print "------- ---------- -------- ------ -------"
print "\n"
for i in self.elements:
if len(i) < 4:
print i[0], " ", i[1][0], " ", i[1][1], " ", i[2], " ", "NONE"
else:
print i[0], " ", i[1][0], " ", i[1][1], " ", i[2], " ", i[3]
def sctassign(self, label, slabel, elabel, dlabel):
for i in s1.sections: # s1 reference
if label == i[0]:
break
return "Section %s does not exist!" %label
j = 0
while slabel + j*dlabel <= elabel:
for i in self.elements:
if i[0] == slabel + j*dlabel:
i.append(label)
j += 1
continue
else:
return "Element %e does not exist!" %(slabel + j*dlabel)
我把那裏似乎是一個問題的Element類內部#nd參考。對不起,代碼有點長...(#nd引用註釋出現的那行是NameError行)
我沒有得到elgen和nd.nodes之間的連接。 el.elgen如何知道甚至存在,訪問nd.nodes?有一些隱藏的全球狀態嗎? – DSM 2012-02-12 17:28:29
其他文件的代碼是相關的,因爲它聽起來就像是生成NameError的地方(雖然它有點不清楚)。btw,'從foo import *'是一個可怕的想法。 – geoffspear 2012-02-12 17:29:11
請發佈其他文件的代碼。這是必要的,以避免猜測:) – 2012-02-12 17:29:21