簡體   English   中英

如何在運行時創建對象並將其引用名稱保存在列表中

[英]How to create an object in runtime and save its reference name in a list

我創建了一個名為“runtime”的類,我想在運行時創建這個類的幾個實例

class runtime:
    products = []
    number_of_objects = 0
    max_objects = 10

    def __init__(self, name):
        self.name = name
        print(self.name)
        runtime.number_of_objects += 1

    def get_name(self):
        return self.name

    @classmethod
    def list_of_products(cls):
        for i in cls.products:
            print(i)

while (True):
    print("1 create")
    print("2 view")
    print("3 delete")

    operation = input("enter your choice:")

    if int(operation) == 1:
        nick_name = input("enter nickname of the product:")
        runtime.products.append(nick_name)
        nick_name = runtime(nick_name)

    elif int(operation) == 2:
        runtime.list_of_products()
        runtime.products[0].get_name()

    elif int(operation) == 3:
        pass

    else:
        print("choice unknown")

但是,在視圖操作期間,當我嘗試獲取我創建的實例的名稱時,出現錯誤:

Traceback (most recent call last):
  File "runtime objects.py", line 34, in <module>
    eval(runtime.products[0]).get_name()
  File "<string>", line 1, in <module>
NameError: name 'object1' is not defined

當我省略 eval() 函數時,它返回 AttributeError

更改變量引用的對象不會更改它引用的前一個對象的值。

所以當你在做:

nick_name = input("enter nickname of the product:")
runtime.products.append(nick_name)
nick_name = runtime(nick_name)

您所做的就是將一些字符串添加到runtime.products列表中,然后讓nick_name變量指向一個從未使用過的新runtime對象。

如果要創建runtime對象列表,只需替換行的順序:

nick_name = input("enter nickname of the product:")
nick_name = runtime(nick_name)
runtime.products.append(nick_name)

或者干脆:

runtime.products.append(runtime(input("enter nickname of the product:")))

現在創建一個runtime對象,然后將其添加到列表中。

這是必要的兩個修改。
第一的。 在創建操作上修改追加的順序

 nick_name = input("enter nickname of the product:")
 nick_name = runtime(nick_name)
 runtime.products.append(nick_name)

第二。 視圖操作( int(operation) == 2 )的意義是獲取類的名稱,因為這是添加要選擇的類所必需的。
為此添加:

    number_product = input("enter number of the product:")

修改的類是:

class runtime:
    products = []
    number_of_objects = 0
    max_objects = 10

    def __init__(self, name):
        self.name = name
        print(self.name)
        runtime.number_of_objects += 1

    def get_name(self):
        return self.name

    @classmethod
    def list_of_products(cls):
        for j, i in enumerate(cls.products):
            print(j, i)

while True:
    print("1 create")
    print("2 view")
    print("3 delete")

    operation = input("enter your choice:")

    if int(operation) == 1:
        nick_name = input("enter nickname of the product:")
        nick_name = runtime(nick_name)
        runtime.products.append(nick_name)


    elif int(operation) == 2:
        runtime.list_of_products()
        number_product = input("enter number of the product:")
        print(runtime.products[int(number_product)].get_name())

    elif int(operation) == 3:
        pass

    else:
        print("choice unknown")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM