【发布时间】:2009-10-30 18:41:06
【问题描述】:
我似乎无法掌握工厂的正确概念。
谁能帮我编写一个简单的测试代码?我在互联网上阅读了一些文本,但无法以相同的方式对其进行编码。其实我无法理解这个过程。复制代码很容易,但我需要了解为什么这行不通。
class Factory:
def __init__(self):
self.msg = "teste"
def fabricateAnotherObject(self,obj,**kwargs):
return apply(obj,**kwargs)
class testClass:
def __init__(self,nome,salario,endereco):
self.nome = nome
self.salario = salario
self.endereco = endereco
def __str__(self):
return "Nome: " + str(self.nome) + "\nEndereco: " + str(self.endereco) + "\nSalario: " + str(self.salario)
a = Factory()
emp = a.fabricateAnotherObject(testClass,"George",2000,"Three Four Five Avenue")
print str(emp)
【问题讨论】:
-
您对
apply()的使用是错误的(它不接受关键字参数),但无论如何apply()已被弃用,使用obj(**kwargs)语法。