如何将方法的return语句存储在变量中? (蟒蛇)

时间:2019-08-06 19:39:07

标签: python oop

我对OOP(以及python)非常陌生 因此,我正在构建类似于Enigma机器的东西。 我的问题是: 我不知道如何访问randomizer方法的return语句。 我需要它来获取随机字符列表。

class generators():

    global static_alphabet,backup_alphabet,encripting_dict,decripting_dict, mutable_alphabet

    static_alphabet=['a','á', 'b', 'c', 'd', 'e','é', 'f', 'g', 'h', 'i','í', 'j', 'k', 'l', 'm', 'n', 'o','ó','ö','ő', 'p', 'q', 'r', 's', 't', 'u','ú','ü','ű','v', 'w', 'x', 'y', 'z',"'",'"','@',':','_','.','-',',','!']
    backup_alphabet=['a','á', 'b', 'c', 'd', 'e','é', 'f', 'g', 'h', 'i','í', 'j', 'k', 'l', 'm', 'n', 'o','ó','ö','ő', 'p', 'q', 'r', 's', 't', 'u','ú','ü','ű', 'v', 'w', 'x', 'y', 'z',"'",'"','@',':','_','.','-',',','!']
    mutable_alphabet=[]

    def randomizer(self):

        mutable_alphabet=[]
        import random

        for element in static_alphabet:

            randletter=backup_alphabet[random.randint(0,len(backup_alphabet)-1)]

            while randletter==element:

                randletter=backup_alphabet[random.randint(0,len(backup_alphabet)-1)]
                if randletter!=element:
                    break

            mutable_alphabet.append(randletter)
            backup_alphabet.remove(randletter)
        return mutable_alphabet

1 个答案:

答案 0 :(得分:0)

基本上,您正在尝试访问def内的函数(class)内的return语句。虽然您的代码当前似乎并未构造为利用OOP,但以下是在同一文件中运行该函数的方式:

class generators():
    ...your code...

gen = generators()
outputArray = gen.randomizer()

# confirm output by printing it out
print( outputArray )

正如我在上面试图说明的那样,这需要放置在类之外。