0
class Client():
    def __init__(self, nome, morada, zona, pais):
        self.Nome = nome
        self.Morada = morada
        self.Zona = zona
        self.Pais = pais
        ...

    def save(self):
         ins = db.insert('tdemo', nome = self.Nome, morada = self.Morada, zona = self.Zona, pais = self.Pais)   
         if not ins:
             return False

if form.validate():
     client = Client(form.Nome.data, form.Morada.data, form.Zona.data, form.Pais.data)
     client.save()

此代码适用于 4 个参数,但实际情况有很多(>15 个参数)。任何人都有想法在不定义每个参数的情况下自动执行此操作?

4

2 回答 2

0

您可以为任意数量的参数扩展以下方法:

parameters = {
    'nome': form.Nome.data,
    'morada': form.Morada.data,
    'zona': form.Zona.data,
    'pais': form.Pais.data,
}
client = Client(**parameters)

这应该保持代码合理,同时仍然明确传递给Client.

于 2013-11-12T12:53:37.013 回答
0

通过整个表格:

class Client():
    def __init__(self, form):
        self.Nome = form.nome.data
        self.Morada = form.morada.data
        self.Zona = form.zona.data
        self.Pais = form.pais.data

client = Client(form)
于 2013-11-12T12:59:47.077 回答