Py学习  »  Python

在Python中从数据库调用类和函数名

Kabomi • 3 年前 • 1299 次点击  

我有大约20个python函数类:

class class_1:
    def display():
        return('This is 1st Class')

class class_2:
    def display():
        return('This is 2nd Class and some more lines')

#Like this 20 class I have

现在,何时调用存储在数据库中的类:

标签 地位
ab 2. 0
光盘 1.
ef 1. 0
生长激素 1. 1.

现在,当我运行以下代码时:

data = db_fetchquery("SELECT class FROM table WHERE status = '0'")

for row in data:
    display = class_{row[0]}.display()
    print(display)

我期待的结果是:

这是二等舱,还有更多的线路

这是头等舱

但我越来越 SyntaxError:无效语法

我还试过字典:

data = db_fetchquery("SELECT class FROM table WHERE status = '0'")

for row in data:
   id = {'x' : 'class_'+row[0]}
   display = id['x'].display()

这让我 AttributeError:“str”对象没有“display”属性

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128727
 
1299 次点击  
文章 [ 1 ]  |  最新文章 3 年前