我正在尝试用Python创建一个CRM,作为课程的最后一个项目
我创建了一个字典,就像我的CRM中的“数据库”一样使用
首先,我尝试在类外更新dict:
index_db = {}
index_db[len(index_db)] = {'name_first': 'Johnny', 'name_last': 'Quest', 'email': 'jquest@cartoonville.com', 'phone': '1 365 999999999'}
index_db[len(index_db)] = {'name_first': 'Scooby', 'name_last': 'Doo', 'email': 'sdoo@cartoonville.com', 'phone': '1 365 888888888'}
index_db[len(index_db)] = {'name_first': 'Homer', 'name_last': 'Simpson', 'email': 'hsimpson@cartoonville.com', 'phone': '1 365 777777777'}
这是回报:
{
0: {
'name_first': 'Johnny',
'name_last': 'Quest',
'email': 'jquest@cartoonville.com',
'phone': '1 365 999999999'
},
1: {
'name_first': 'Scooby',
'name_last': 'Doo',
'email': 'sdoo@cartoonville.com',
'phone': '1 365 888888888'
},
2: {
'name_first': 'Homer',
'name_last': 'Simpson',
'email': 'hsimpson@cartoonville.com',
'phone': '1 365 777777777'
}
}
它看起来很棒,所以我创建了一个类:
class Consumer(object):
index_db = {}
args = {'name_first': None, 'name_last': None, 'email': None, 'phone': None}
def __set__(self, var, val):
self.args[var] = val
def __insert__(self):
self.index_db[len(self.index_db)] = self.args
并插入三个消费者:
consumer = Consumer()
consumer.__set__('name_first', 'Johnny')
consumer.__set__('name_last', 'Bravo')
consumer.__set__('email', 'jbravo@cartoonville.com')
consumer.__set__('phone', '1 353 30316541')
consumer.__insert__()
consumer.__set__('name_first', 'Dexter')
consumer.__set__('name_last', 'Scientist')
consumer.__set__('email', 'dscientist@cartoonville.com')
consumer.__set__('phone', '1 353 33256001')
consumer.__insert__()
consumer.__set__('name_first', 'Barney')
consumer.__set__('name_last', 'Gumble')
consumer.__set__('email', 'bgumble@cartoonville.com')
consumer.__set__('phone', '1 353 555961533')
consumer.__insert__()
这是回报:
{
0: {
'email': 'bgumble@cartoonville.com',
'name_first': 'Barney',
'name_last': 'Gumble',
'phone': '1 353 555961533'},
1: {
'email': 'bgumble@cartoonville.com',
'name_first': 'Barney',
'name_last': 'Gumble',
'phone': '1 353 555961533'},
2: {
'email': 'bgumble@cartoonville.com',
'name_first': 'Barney',
'name_last': 'Gumble',
'phone': '1 353 555961533'
}
}
哦,天哪,为什么这不管用
一个快速而肮脏的解决方法是:
deepcopy
为您创建一个新字典真的,你需要一个更好的类接口
正如@Hai Vu所说:dunder的方法是什么
这样可能更好。是的,我知道它需要更多的台词:
请注意,由于
index_db
是类的一个成员,所以整个过程也可能在类级别,所以@classmethod
就是这样的这是另一种做事的方法。这会将所有内容移到
__init__
方法。现在Consumer
有一个属性index
,每当创建一个新的Consumer
时它就会更新这个属性现在之后
Consumer.index
将等于正如@quamrana指出的,您的代码一直在重用
self.args
的同一个副本。另一个快速而肮脏的修复方法是在insert之后立即重置self.args
:最后一行创建一个新字典,准备填充
顺便问一下,dunder(双下划线)是怎么回事
相关问题 更多 >
编程相关推荐