2017-12-28 10 views
1


私は、コースの最終プロジェクトとして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' 
    } 
} 

ああ、神様を、なぜこの作品ではないのですか?

+1

これは、同じ辞書内の要素を上書きするため、クラスメンバとして一度作成する 'args'です。 – quamrana

+1

すべての「コンシューマ」オブジェクトは1つの辞書を共有します。そのうち1つを変更すると、そのすべてが変更されます。 '__init__'メソッドを定義してください。代わりにインスタンス変数を設定します。 –

答えて

1

迅速かつ汚い修正はこれです:

import copy 
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.args = copy.deepcopy(self.args) 

deepcopyはあなたのための新しい辞書を作成します。

本当に、あなたのクラスへのより良いインターフェースが必要です。

と@Hai Vuと言う:ダンドの方法は何ですか?

これはもっと良いかもしれません。そして、はい、私はそれがより多くの行を使用する必要がある知っている:index_dbは、クラスのメンバーであるので、それは@classmethod秒で何ですので、全部は同様に、クラスレベルであるかもしれない

class Consumer(object): 
    index_db = {} 

    @classmethod 
    def reset(cls): 
     cls.args = {'name_first': None, 'name_last': None, 'email': None, 'phone': None} 

    @classmethod 
    def set(cls, var, val): 
     cls.args[var] = val 

    @classmethod 
    def insert(cls): 
     cls.index_db[len(cls.index_db)] = cls.args 

consumer = Consumer 

consumer.reset() 
consumer.set('name_first', 'Johnny') 
consumer.set('name_last', 'Bravo') 
consumer.set('email', '[email protected]') 
consumer.set('phone', '1 353 30316541') 
consumer.insert() 

注こと。

1

@quamranaが指摘したように、コードはself.argsの同じコピーを再利用し続けます。別の迅速かつ汚い修正は、挿入後self.args権利をリセットすることです:

def __insert__(self): 
     self.index_db[len(self.index_db)] = self.args 
     self.args = {'name_first': None, 'name_last': None, 'email': None, 'phone': None} 

最後の行が移入される準備ができて新しい辞書を作成します。

ところで、ダンダー(二重の下線)とは何ですか?

0

これは別の方法です。これにより、すべてが__init__メソッドに移動します。 Consumerには、Consumerが作成されるたびに更新される属性indexがあります。

Consumer('Johnny', 'Bravo', '[email protected]', '1 353 30316541') 
Consumer('Dexter', 'Scientist', '[email protected]', '1 353 33256001') 
Consumer('Barney', 'Gumble', '[email protected]', '1 353 555961533') 

Consumer.index

class Consumer(dict): 
    index={} 
    _count=0 
    def __init__(self, first=None, last=None, email=None, phone=None): 
    super().__init__(name_first=first, name_last=last, email=email, phone=phone) 
    Consumer.index[Consumer._count] = self 
    Consumer._count += 1 

{0: {'email': '[email protected]', 
    'name_first': 'Johnny', 
    'name_last': 'Bravo', 
    'phone': '1 353 30316541'}, 
1: {'email': '[email protected]', 
    'name_first': 'Dexter', 
    'name_last': 'Scientist', 
    'phone': '1 353 33256001'}, 
2: {'email': '[email protected]', 
    'name_first': 'Barney', 
    'name_last': 'Gumble', 
    'phone': '1 353 555961533'}} 
0

みんなありがとうと同じになります!

私は@パトリック・ハウ先端に続く、と私の辞書をクリアするのinitを作成し、そして再びのinitを呼び出した後に、消費者を挿入します。汚いとクールなソリューションのための感謝@quamrana

class Consumer(object): 
    index_db = {} 

    def __init__(self): 
    Consumer.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 
    self.__init__() 

:このように

@ hai-vu:dunderは名前set()を使用できないため、すべてのdefに2つのアンダースコアを入れて、醜いデフォルトのkkkを作成します。 。

もう一度、ありがとうございました!

関連する問題