在函数中设置变量并导入它们

时间:2016-06-05 09:02:22

标签: python

我正在写一个冒险游戏,我正在用不同的文件编写函数,例如saveGame函数和read函数。有4个主要文件,全部互连。主要是agMain.py。我的问题是我导入了其他一个文件agRead.py,并在名为read()的函数内调用该函数。以下是agRead.py的数据:

import os
import operator
import sys
import agSave
import agCreation
def read():
    while True:
        try:
            file = open("agData.txt", "r")
        except:
            exec(open("agCreation.py").read())
            break
        break
    file = open("agData.txt", "r")
    data = file.readlines(0)
    encrypFName = data[0]
    encrypSName = data[1]
    encrypAge = data[2]
    encrypGender = data[3]
    encrypHealth = data[4]
    encrypMaxHealth = data[5]
    encrypArmour = data[6]
    encrypMaxArmour = data[7]
    encrypHealthPotions = data[8]
    encrypExp = data[9]
    encrypMaxExp = data[10]
    encrypLevel = data[11]
    encrypGold = data[12]
    encrypMaxGold = data[13]
    encrypPowerLevel = data[14]
    encrypMaxPowerExp = data[15]
    encrypPowerExp = data[16]
    encrypStage = data[17]
    fName = encrypFName.strip()
    sName = encrypSName.strip()
    age = encrypAge.strip()
    gender = encrypGender.strip()
    health = encrypHealth.strip()
    maxHealth = encrypMaxHealth.strip()
    armour = encrypArmour.strip()
    maxArmour = encrypArmour.strip()
    healthPotions = encrypHealthPotions.strip()
    exp = encrypExp.strip()
    maxExp = encrypMaxExp.strip()
    level = encrypLevel.strip()
    gold = encrypGold.strip()
    maxGold = encrypMaxGold.strip()
    powerLevel = encrypPowerLevel.strip()
    maxPowerExp = encrypMaxPowerExp.strip()
    powerExp = encrypPowerExp.strip()
    stage = encrypStage.strip()

如您所见,它设置了stagelevel等变量。但是我的agMain个文件:

import os
import operator
import sys
import agSave
import agRead
while True:
    agRead.read()
    if (stage == 1):
        exec(open("agStage1.py").read())
    elif (stage == 2):
        exec(open("agStage2.py").read())
    elif (stage == 3):
        exec(open("agStage3.py").read())
    elif (stage == 4):
        exec(open("agStage4.py").read())
    elif (stage == 5):
        exec(open("agStage5.py").read())
    elif (stage == 6):
        exec(open("agStage6.py").read())
    elif (stage == 7):
        exec(open("agStage7.py").read())
    elif (stage == 8):
        exec(open("ageStage8.py").read())
    elif (stage == 9):
        exec(open("agStage9.py").read())
    elif (stage == 10):
        exec(open("agStage10.py").read())
    elif (stage > 10):
        for i in range(100):
            print("Game over!")
        print("****************")
        print("Well done, you have completed the game!")
        exit()
    else:
        print("An error has occured.")
        continue

正如您在此处所看到的,它在代码中的函数中使用stage。但它不能。如果我运行它,它表示阶段没有定义,就像我没有运行该功能。是否可以在函数中设置变量,使用该函数,并在另一个文件中使用creat中的变量?

修改

在我做这个的时候,我不懂课,所以我道歉。每个遇到类似问题的人(在函数之间共享变量)只是创建一个具有所需函数的类作为方法。它省去了很多麻烦!

6 个答案:

答案 0 :(得分:1)

https://docs.python.org/3/tutorial/controlflow.html#defining-functions

  

更确切地说,函数中的所有变量赋值都将值存储在本地符号表中;而变量引用首先在本地符号表中查找,然后在封闭函数的本地符号表中查找,然后在全局符号表中查找,最后在内置名称表中查找。因此,全局变量不能直接在函数内赋值(除非在global语句中命名),尽管它们可能被引用。

分配给函数中的变量会隐式地在该函数中创建局部变量,除非您已将其声明为global

答案 1 :(得分:1)

您需要了解更多关于scoping的信息。在函数内部,由赋值语句绑定的名称在函数调用的本地名称空间中创建。在进行函数调用时会创建此命名空间,并使用绑定到其名称的参数值进行初始化。当函数返回(或引发异常)时,名称空间将被删除,名称将不再可用。

由于您想要阅读许多不同的值,请考虑创建类的实例。实例也有一个命名空间,在该命名空间中绑定的名称将一直可用,直到被删除或者对象被垃圾回收为止。

在@Merlin发表他的有用答案后,我决定更新这个答案,包括一个数据驱动的版本,转载如下(但没有经过测试)。考虑这一点可能会导致您创建一个可以创建/初始化几种不同类型对象的函数。

由于执行速度不太可能是一个问题,我更喜欢一种更通用的方法。

class Conf(object):
    pass

def read():
    while True:
        try:
            file = open("agData.txt", "r")
        except:
            exec(open("agCreation.py").read())
            break
        break
    file = open("agData.txt", "r")

    conf = Conf()

    names = ["fName", "sName", "age", "gender", "health", "maxHealth",
         "armour", "maxArmour", "healthPotions", "exp", "maxExp",
         "level", "gold", "maxGold", "powerLevel", "maxPowerExp",
         "powerExp", "stage"]
    for name in names:
        setattr(conf, name, next(file).strip()
    return conf

答案 2 :(得分:1)

通过使用链接,您可以减少使用的行数并提高可读性。

  class Conf(object):
        pass

def read():
    while True:
        try:
            file = open("agData.txt", "r")
        except:
            exec(open("agCreation.py").read())
            break
        break
    file = open("agData.txt", "r")
    data = file.readlines(0)

    conf = Conf()

    conf.fName     = data[0].strip()
    conf.sName     = data[1].strip()
    conf.age       = data[2].strip()
    conf.gender    = data[3].strip()
    conf.health    = data[4].strip()
    conf.maxHealth = data[5].strip()
    conf.armour    = data[6].strip()
    conf.maxArmour = data[7].strip()
    conf.healthPotions = data[8].strip()
    conf.exp       = data[9].strip()
    conf.maxExp    = data[10].strip()
    conf.level     = data[11].strip()
    conf.gold      = data[12].strip()
    conf.maxGold   = data[13].strip()
    conf.powerLevel = data[14].strip()
    conf.maxPowerExp =data[15].strip()
    conf.powerExp  = data[16].strip()
    conf.stage     = data[17].strip()
    return conf

答案 3 :(得分:0)

变量仅在read内可见。

通过执行此类操作来包含您要使用的变量。

agRead.py

class Conf(object):
    pass


def read():
    while True:
        try:
            file = open("agData.txt", "r")
        except:
            exec(open("agCreation.py").read())
            break
        break
    file = open("agData.txt", "r")
    data = file.readlines(0)
    encrypFName = data[0]
    encrypSName = data[1]
    encrypAge = data[2]
    encrypGender = data[3]
    encrypHealth = data[4]
    encrypMaxHealth = data[5]
    encrypArmour = data[6]
    encrypMaxArmour = data[7]
    encrypHealthPotions = data[8]
    encrypExp = data[9]
    encrypMaxExp = data[10]
    encrypLevel = data[11]
    encrypGold = data[12]
    encrypMaxGold = data[13]
    encrypPowerLevel = data[14]
    encrypMaxPowerExp = data[15]
    encrypPowerExp = data[16]
    encrypStage = data[17]

    conf = Conf()

    conf.fName = encrypFName.strip()
    conf.sName = encrypSName.strip()
    conf.age = encrypAge.strip()
    conf.gender = encrypGender.strip()
    conf.health = encrypHealth.strip()
    conf.maxHealth = encrypMaxHealth.strip()
    conf.armour = encrypArmour.strip()
    conf.maxArmour = encrypArmour.strip()
    conf.healthPotions = encrypHealthPotions.strip()
    conf.exp = encrypExp.strip()
    conf.maxExp = encrypMaxExp.strip()
    conf.level = encrypLevel.strip()
    conf.gold = encrypGold.strip()
    conf.maxGold = encrypMaxGold.strip()
    conf.powerLevel = encrypPowerLevel.strip()
    conf.maxPowerExp = encrypMaxPowerExp.strip()
    conf.powerExp = encrypPowerExp.strip()
    conf.stage = encrypStage.strip()
    return conf

agMain.py

import agRead
while True:
    conf = agRead.read()
    if conf.stage == 1:

答案 4 :(得分:0)

是的,但是变量只有在执行函数后才可用:

### file agRead.py ###
def read():
    # some code there
    read.my_super_var = 'value'

### file agMain.py ###
import agRead

agRead.read()
print(agRead.read.my_super_var) # value

答案 5 :(得分:0)

好的,我已经解决了这个问题。感谢所有回答的人,特别是holdenweb和nightcrawler。我在agRead.py中创建了一个类,并在返回类之前将所有变量作为该类的一部分。这是我的agRead.py现在:

import os
import operator
import sys
import agSave
import agCreation
class Conf(object):
    pass

# READ FUNCTION
def read():
    try:
        open("agData.txt", "r")
    except:
        exec(open("agCreation.py").read())
    file = open("agData.txt", "r")
    data = file.readlines(0)
    encrypFName = data[0]
    encrypSName = data[1]
    encrypAge = data[2]
    encrypGender = data[3]
    encrypHealth = data[4]
    encrypMaxHealth = data[5]
    encrypArmour = data[6]
    encrypMaxArmour = data[7]
    encrypHealthPotions = data[8]
    encrypExp = data[9]
    encrypMaxExp = data[10]
    encrypLevel = data[11]
    encrypGold = data[12]
    encrypMaxGold = data[13]
    encrypPowerLevel = data[14]
    encrypMaxPowerExp = data[15]
    encrypPowerExp = data[16]
    encrypStage = data[17]

    conf = Conf()

    conf.fName = encrypFName.strip()
    conf.sName = encrypSName.strip()
    conf.age = encrypAge.strip()
    conf.gender = encrypGender.strip()
    conf.health = encrypHealth.strip()
    conf.maxHealth = encrypMaxHealth.strip()
    conf.armour = encrypArmour.strip()
    conf.maxArmour = encrypArmour.strip()
    conf.healthPotions = encrypHealthPotions.strip()
    conf.exp = encrypExp.strip()
    conf.maxExp = encrypMaxExp.strip()
    conf.level = encrypLevel.strip()
    conf.gold = encrypGold.strip()
    conf.maxGold = encrypMaxGold.strip()
    conf.powerLevel = encrypPowerLevel.strip()
    conf.maxPowerExp = encrypMaxPowerExp.strip()
    conf.powerExp = encrypPowerExp.strip()
    conf.stage = encrypStage.strip()
    return conf

和我的agMain.py:

 # Main
    # Will open a stage depending on the stage the user is at.
    import os
    import operator
    import sys
    import agSave
    import agRead
    #Read the file
    while True:
        agRead.read()
        if (conf.stage == 1):
            exec(open("agStage1.py").read())
        elif (conf.stage == 2):
            exec(open("agStage2.py").read())
        elif (conf.stage == 3):
            exec(open("agStage3.py").read())
        elif (conf.stage == 4):
            exec(open("agStage4.py").read())
        elif (conf.stage == 5):
            exec(open("agStage5.py").read())
        elif (conf.stage == 6):
            exec(open("agStage6.py").read())
        elif (conf.stage == 7):
            exec(open("agStage7.py").read())
        elif (conf.stage == 8):
            exec(open("ageStage8.py").read())
        elif (conf.stage == 9):
            exec(open("agStage9.py").read())
        elif (conf.stage == 10):
            exec(open("agStage10.py").read())
        elif (conf.stage > 10):
            for i in range(100):
                print("Game over!")
            print("****************")
            print("Well done, you have completed the game!")
            exit()
        else:
            print("An error has occured.")
            continue

这完美无缺,谢谢大家!这是一个了不起的社区,我希望成为它的真正成员!再次感谢!