how to user super() in python class inheritance
本问题已经有最佳答案,请猛点这里访问。
我正试图根据这个答案实现
我有以下课程:
1 2 3 4 5 6 7 8 9 10 11 | class Collection(): """returns a collection curser from mongodb""" def __init__(self, db, collection_name): self.db = db self.collection_name = collection_name if not hasattr(self.__class__, 'client'): self.__class__.client = MongoClient() self.data_base = getattr(self.client, self.db) self.collection = getattr(self.data_base, self.collection_name) |
以及以下子类:
1 2 3 | class User(Collection): def __init__(self, db, collection_name): super(User, self).__init__(db, collection_name) |
打电话给
1 | agents = Collection('hkpr_restore','agents') |
调用子类:
1 | user = User('hkpr_restore','agents') |
我得到了一个错误:
1 2 3 4 5 6 | Traceback (most recent call last): File"main.py", line 37, in <module> user = User('hkpr_restore','agents') File"filepath/user.py", line 35, in __init__ super(User, self).__init__(db, collection_name) TypeError: must be type, not classobj |
我做错什么了?
型
从像