Pythonでクラスを作成し、__init__
クラスメソッドで変数を呼び出しました。一部の init 変数でエラーが発生します。
以下はクラスです:
class MyRedisClass(object):
def __init__(self):
self.DEFAULT_PAGE_SIZE = 10
# the line below gives an error - global name 'pool' is not defined
# if the below line is commented, I can get the value of DEFAULT_PAGE_SIZE inside the some_function
self.pool = redis.ConnectionPool(host='XXX.XXX.XX.XX', port=XXXX, db=0)
self.redis_connection = redis.Redis(connection_pool=pool)
def some_function(self, some_data):
print self.DEFAULT_PAGE_SIZE
pipeline = self.redis_connection.pipeline()
it = iter(some_data)
for member, score in zip(it, it):
pipeline.zadd(leaderboard_name, member, score)
pipeline.execute()
ターミナルで、次のようにクラスのインスタンスを作成します-
mklass = MyRedisClass()
mklass.some_function(['a', 1])
指摘したように、エラーが発生します-global name 'pool' is not defined
上記のクラス宣言の何が問題になっていますか?
プールを宣言している場所で NameError が発生するのはなぜですか?
より良いクラス定義のために、何かをする必要がありますsuper
かclassmethod
?