简体   繁体   English

AttributeError:类型 object 'database' 没有属性 'initialize'

[英]AttributeError: type object 'database' has no attribute 'initialize'

C:\Users\Lenovo\AppData\Local\Programs\Python\Python37-32\fullstack\terminal\Scripts\python.exe C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/terminal/mongo1.py
Traceback (most recent call last):
  File "C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/terminal/mongo1.py", line 5, in <module>
    database.initialize()
AttributeError: type object 'database' has no attribute 'initialize'

Process finished with exit code 1

its giving me this error after running the below code please help im still new in python programming I have checked but I don't see where I dii it wrong!在运行以下代码后它给了我这个错误请帮助我在 python 编程中仍然是新的我已经检查过,但我看不出我哪里出错了!

class database(object):
    uri = "mongodb://127.0.0.1:27017"
    database = None


@staticmethod
def initialize():
    client = pymongo.MongoClient(database.uri)
    database.database = client["samaz"]



@staticmethod
def insert(collection, data):
    database.database[collection].insert(data)


@staticmethod
def find(collection, query):
    return database.database[collection].find(query)


@staticmethod
def find_one(collection, query):
   return database.database[collection].find_one(query)

You need to declare your methods inside the class, like so:您需要在 class 中声明您的方法,如下所示:

class Database:
    uri = ...

    def  __init__(self):
        self.client = pymongo.MongoClient(self.uri)
        self.database = self.client["samaz"]

To me it also looks like you want to have those other methods inside the class, without @staticmethod decorator.对我来说,您似乎还想在 class 中使用这些其他方法,而无需 @staticmethod 装饰器。 It is used for methods that don't use class nor instance itself, basically using current class only as namespace.它用于不使用 class 或实例本身的方法,基本上仅使用当前 class 作为命名空间。

It looks like the problem is that most of the class definition is not indented as it should be.看起来问题是大多数 class 定义没有按应有的缩进。 Remember that in python indentation is crucial, and is why braces {} are not required.请记住,在 python 中,缩进至关重要,这也是为什么不需要大括号 {} 的原因。 This works for me error-free:这对我来说没有错误:

class database(object):
    uri = "mongodb://127.0.0.1:27017"
    database = None


    @staticmethod
    def initialize():
        client = pymongo.MongoClient(database.uri)
        database.database = client["samaz"]



    @staticmethod
    def insert(collection, data):
        database.database[collection].insert(data)


    @staticmethod
    def find(collection, query):
        return database.database[collection].find(query)


    @staticmethod
    def find_one(collection, query):
       return database.database[collection].find_one(query)

x = database()
print(x)

Also your traceback shows you using Python 3.x, in which inheriting from object is now done by default so no need to do that.此外,您的回溯显示您使用 Python 3.x,其中从 object 继承现在默认完成,因此无需这样做。

Also see Edvard's post.另见爱德华的帖子。 This will only stop you getting a traceback but he raises an important point about your class design.这只会阻止您获得回溯,但他提出了关于您的 class 设计的重要观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM