简体   繁体   English

模块在Python没有属性

[英]Module has no attribute in Python

I am importing variables from a module, but it says that there is no instance of the variable that has been imported in. There are two files: eqparse and main.我正在从模块导入变量,但它说没有导入的变量实例。有两个文件:eqparse 和 main。 Ill include all the miscellaneous code (that likely isn't related) just in case.为了以防万一,我将包括所有杂项代码(可能不相关)。 There are comments to help navigate the code.有注释可帮助导航代码。

The main code:主要代码:

import graphics
import eqparse
# NOTE: For equation mapping, it will input a string and convert the data to a table. 
# After, you take in the table coords and map each point 


# dump vars here #
xypm = 0

counter = eqparse.counter

patternFind = eqparse.patternFind

indexlist = eqparse.indexlist

# profile initialization #
def init(name, nxsize, nysize, noffset, nzoom):
    global zoom
    zoom = nzoom
    global xsize
    xsize = nxsize
    global eqtype
    eqtype = bool(1)
    global ysize
    ysize = nysize
    global win
    win = GraphWin(name, nxsize, nysize)
    global offset
    offset = noffset
    global xymark
    xymark = (nzoom * -1)/2 
    global txsize
    txsize = nxsize/2
    global tysize
    tysize = nysize/2
    global txymark
    txymark = xymark * -1 + 1
    global xp1
    xp1 = Point(xypm,(txsize) + offset)
    global yp1
    yp1 = Point((tysize) + offset, xypm)
    global xp2
    xp2 = Point(xypm,(txsize) - offset)
    global yp2
    yp2 = Point((tysize) - offset, xypm)

# starting procedure #
def startUp():
        # xy lines #
    global xymark
    global xp1
    global xp2
    global yp1
    global yp2
    global xypm
    xtrace = Line(Point(0,tysize), Point(xsize,tysize))
    ytrace = Line(Point(txsize,0), Point(txsize,ysize))
    xtrace.draw(win)
    ytrace.draw(win)

        # grid drawer #
    while xymark < txymark:
        
        txline = Line(xp1, xp2)
        tyline = Line(yp1, yp2)
        txline.draw(win)
        tyline.draw(win)
        xypm = xypm + xsize/zoom
        xp1 = Point(xypm,(tysize) + offset)
        yp1 = Point((txsize) + offset, xypm)
        xp2 = Point(xypm,(tysize) - offset)
        yp2 = Point((txsize) - offset, xypm)
        xymark = xymark + 1

        # code for ending mark #
    Line(Point(txsize - offset, ysize - 1), Point(txsize + offset, ysize - 1)).draw(win)
    Line(Point(xsize - 1,tysize - offset), Point(xsize - 1, tysize + offset)).draw(win)

# point drawing #
def drawCo(nx, ny):
    pCircle = Circle(Point(xsize/zoom * (nx) + xsize/2, ysize - (ysize/zoom * (ny) + ysize/2)), 3)
    pCircle.setFill("black")
    pCircle.draw(win)


# main #
print("Checkpoint 1")
init("test",500, 500, 10, 20)
print("Checkpoint 2")
startUp()
print("Checkpoint 3")
drawCo(3,5)
print("Checkpoint 4")
patternFind("Dung","DungBeatlesbeingadungbeetle")
print("Checkpoint 5")
print(counter)
print("Checkpoint 6")
for x in range(len(indexlist)):
    print(indexlist[x])
print("Checkpoint 7")

# exit function #
win.getMouse()
win.close()

The eqparse code: eqparse代码:

#   eqparse will look for certain patterns in strings, generate an equation,
#   and create a table that a drawing function can read


global indexlist

global counter

# convert string to valid equation data


def convStr(input):
    if input[0] == 'y':
        print("Linear")
        
    elif input[0] == 'r':
        print("Polar")
    else:
        print("Use Equation type on left side")

# subroutine that will be used to find patterns in a sequence #

def patternFind(pattern, input):

    indexlist = []
    
    counter = 0

    l = len(pattern)

    tstr =""

    if l > len(input):
        pass
    else:
        i = 0
        j = len(input) - len(pattern) + 1
        k=0
        while j > 0:
            tstr = ""
            i=0
            while len(tstr) < l:
                tstr = tstr + input[i + k]
                i = i + 1
            if tstr == pattern:
                counter = counter + 1
                indexlist.append(i+k)
            else: 
                pass
            j = j - 1
            k=k+1

The output/error code:输出/错误代码:

module 'eqparse' has no variable 'counter'

Help of any kind is appreciated.任何形式的帮助表示赞赏。

I tried changing the variable name just in case it was already used in Python's standard library or the graphics library, but nothing changed.我尝试更改变量名,以防它已经在 Python 的标准库或图形库中使用,但没有任何改变。

When you define a variable at the module level,在模块级别定义变量时,
you're defining a global variable.您正在定义一个全局变量。
When you define a variable inside a function,当你在 function 中定义一个变量时,
you're defining a local variable.你正在定义一个局部变量。
if you say foo = 7 on the module level,如果你在模块级别说foo = 7
and then foo = 17 in a function,然后在 function 中foo = 17
you are changing the local variable foo ,您正在更改局部变量foo
which is unrelated to the global variable foo ,这与全局变量foo无关
however, if, before you say foo = 17 in the function,但是,如果在 function 中说foo = 17之前,
you say global foo , in the function,你说global foo ,在 function 中,
then you are saying "when i define foo, i'm redefining the global variable foo".那么你是在说“当我定义 foo 时,我正在重新定义全局变量 foo”。

#   eqparse will look for certain patterns in strings, generate an equation,
#   and create a table that a drawing function can read

indexlist = []
counter = 0


# convert string to valid equation data


def convStr(input):
    if input[0] == 'y':
        print("Linear")
        
    elif input[0] == 'r':
        print("Polar")
    else:
        print("Use Equation type on left side")

# subroutine that will be used to find patterns in a sequence #

def patternFind(pattern, input):
    global indexlist
    global counter

    l = len(pattern)

    tstr =""

    if l > len(input):
        pass
    else:
        i = 0
        j = len(input) - len(pattern) + 1
        k=0
        while j > 0:
            tstr = ""
            i=0
            while len(tstr) < l:
                tstr = tstr + input[i + k]
                i = i + 1
            if tstr == pattern:
                counter = counter + 1
                indexlist.append(i+k)
            else: 
                pass
            j = j - 1
            k=k+1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM