私は、コースの最終プロジェクトとしてPythonでCRMを作成しようとしています。
そして私は自分のCRMの "データベース"フォームのように使う辞書を作成します。クラス関数を使用して辞書を更新する
まず、私はクラスの外dictの更新をしようと試み:
index_db = {}
index_db[len(index_db)] = {'name_first': 'Johnny', 'name_last': 'Quest', 'email': '[email protected]', 'phone': '1 365 999999999'}
index_db[len(index_db)] = {'name_first': 'Scooby', 'name_last': 'Doo', 'email': '[email protected]', 'phone': '1 365 888888888'}
index_db[len(index_db)] = {'name_first': 'Homer', 'name_last': 'Simpson', 'email': '[email protected]', 'phone': '1 365 777777777'}
そして、それの戻り:
{
0: {
'name_first': 'Johnny',
'name_last': 'Quest',
'email': '[email protected]',
'phone': '1 365 999999999'
},
1: {
'name_first': 'Scooby',
'name_last': 'Doo',
'email': '[email protected]',
'phone': '1 365 888888888'
},
2: {
'name_first': 'Homer',
'name_last': 'Simpson',
'email': '[email protected]',
'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
を3つのコンシューマを挿入します。
consumer = Consumer()
consumer.__set__('name_first', 'Johnny')
consumer.__set__('name_last', 'Bravo')
consumer.__set__('email', '[email protected]')
consumer.__set__('phone', '1 353 30316541')
consumer.__insert__()
consumer.__set__('name_first', 'Dexter')
consumer.__set__('name_last', 'Scientist')
consumer.__set__('email', '[email protected]')
consumer.__set__('phone', '1 353 33256001')
consumer.__insert__()
consumer.__set__('name_first', 'Barney')
consumer.__set__('name_last', 'Gumble')
consumer.__set__('email', '[email protected]')
consumer.__set__('phone', '1 353 555961533')
consumer.__insert__()
そして、それは返します:
{
0: {
'email': '[email protected]',
'name_first': 'Barney',
'name_last': 'Gumble',
'phone': '1 353 555961533'},
1: {
'email': '[email protected]',
'name_first': 'Barney',
'name_last': 'Gumble',
'phone': '1 353 555961533'},
2: {
'email': '[email protected]',
'name_first': 'Barney',
'name_last': 'Gumble',
'phone': '1 353 555961533'
}
}
ああ、神様を、なぜこの作品ではないのですか?
これは、同じ辞書内の要素を上書きするため、クラスメンバとして一度作成する 'args'です。 – quamrana
すべての「コンシューマ」オブジェクトは1つの辞書を共有します。そのうち1つを変更すると、そのすべてが変更されます。 '__init__'メソッドを定義してください。代わりにインスタンス変数を設定します。 –