0

Google AppEngineで「DiveIntoPython」を実行しているときに、あるクラスのメソッドを別のクラスから呼び出そうとしたときに、このエラーが発生しました。

ERROR __init__.py:463] create() takes exactly 1 argument (2 given)
Traceback (most recent call last):
  File "main.py", line 35, in get
    dal.create("sample-data");
  File "dataAccess/dal.py", line 27, in create
    self.data_store.create(data_dictionary);
TypeError: create() takes exactly 1 argument (2 given)

これが私のメインクラスです:

# filename: main.py

from dataAccess.dal import DataAccess

class MySampleRequestHandler(webapp.RequestHandler):
"""Configured to be invoked for a specific GET request"""

    def get(self):
        dal = DataAccess();
        dal.create("sample-data"); # problem area

MySampleRequestHandler.get()DataAccess他に定義されているインスタンス化と呼び出しを試みます。

# filename: dal.py

from dataAccess.datastore import StandardDataStore

class DataAccess:
    """Class responsible for wrapping the specific data store"""

    def __init__(self):
        self.data_store = None;

        data_store_setting = config.SETTINGS['data_store_name'];                
        if data_store_setting == DataStoreTypes.SOME_CONFIG:
            self.data_store = StandardDataStore();

        logging.info("DataAccess init completed.");

    def create(self, data_dictionary):
        # Trying to access the data_store attribute declared in __init__
        data_store.create(data_dictionary);

DataAccess.create()特に、Dive into Pythonがクラスメソッドの呼び出しについてどのように説明しているかに応じて、引数に1つのパラメーターを使用して呼び出すことができると思いました。

クラスメソッドを定義するときは、を含む各メソッドの最初の引数として自分自身を明示的にリストする必要があります__init__。クラス内から祖先クラスのメソッドを呼び出すときは、self引数を含める必要があります。ただし、外部からクラスメソッドを呼び出す場合は、self引数に何も指定しません。完全にスキップすると、Pythonによってインスタンス参照が自動的に追加されます。

4

2 に答える 2

3

ではself.data_store.create(data_dictionary)、はメソッドでself.data_store作成されたオブジェクトを指します。self.data_store = StandardDataStore()__init__

オブジェクトのcreateメソッドがStandardDataStore追加の引数を期待していないようです。

于 2011-08-08T19:38:46.543 に答える
1

そのはずself.data_store.create(data_dictionary);

于 2011-08-08T19:15:59.423 に答える