简体   繁体   English

Python中是否有方法的匿名调用?

[英]Is there an anonymous call of a method in Python?

This is my database manager class: 这是我的数据库管理器类:

class DatabaseManager(object):
    def __init__(self):
        super().__init__()
        self.conn = sqlite3.connect('artist.db')
        self.c = self.conn.cursor()

    def create_table(self):
        self.c.execute(
            'CREATE TABLE IF NOT EXISTS artists(title TEXT, artist TEXT, album TEXT, year INTEGER, '
            'genre TEXT, ext TEXT, path TEXT, art TEXT )')
        self.c.close()
        self.conn.close()

    def insert(self, song):
        self.c.execute(
            'INSERT INTO artists(title, artist, album, year, genre, ext, path, art) VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
            (song.title, song.artist, song.album, song.year, song.genre, song.ext, song.path, song.art))
        self.conn.commit()
        self.c.close()
        self.conn.close()

    def retrieve_all(self):
        self.c.execute('SELECT * FROM artists')
        data = self.c.fetchall()
        self.c.close()
        self.conn.close()
        return data

There are more methods but this is not important. 有更多方法,但这并不重要。 When i start the app i call create_table() and when i need some database action i create another obect(or remake the past one) to call insert for example. 当我启动应用程序时,我调用create_table() ,当我需要一些数据库操作时,我创建了另一个对象(或重新制作了过去的对象)以调用例如insert

    db = DatabaseManager()
    print(db.retrieve_artist('asdf'))
    db = DatabaseManager()
    print(db.retrieve_all())

Is there any way to avoid this? 有什么办法可以避免这种情况? In Java there is anonymous calls: new DatabaseManager().retrieve_artist("asdf") Is anything similar possible in Python? 在Java中,存在匿名调用: new DatabaseManager().retrieve_artist("asdf")在Python中是否有类似的可能?

As @Remcogerlich said, you are assigning the instantiation to a variable. 正如@Remcogerlich所说,您正在将实例化分配给变量。 If you just don't do so, you can approach what you want. 如果您只是不这样做,则可以按照自己的意愿进行操作。

DatabaseManager().retrieve_artist('asdf')

AnyOtherClass().callAnyMethod()

# both disappeared 

In this way, Python executes the instantiation, calls the method you want to, and since you don't tell it to assign to any variable, it " disappears ". 这样,Python执行实例化,调用您想要的方法,并且由于您不告诉它分配给任何变量,因此它“ 消失了 ”。

You may even do that with class functions, like this: 您甚至可以使用类函数来执行此操作,如下所示:

ClassWithClassMethods.callAnyClassMethod()

However, I wouldn't recommend you to work with that on that way. 但是,我不建议您采用这种方式。 Normally, database connections are established as an instantiation, used as long as you need to, and when you don't need them anymore you close them safely . 通常,数据库连接被建立为实例化,并在需要时使用,当不再需要它们时,可以安全地关闭它们。 I think you are not doing that there, and are wasting memory usage, I guess (anyone correct me if I'm wrong). 我想我想您没有在那做,并且浪费了内存使用(如果我错了,任何人都可以纠正我)。 It's easier that you create an object for each time you want to use the database for long, and once you finish in that piece of code, you close the connection and remove the object. 每次想要长时间使用数据库时,创建一个对象会更容易,一旦完成这段代码,就可以关闭连接并删除该对象。

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

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