简体   繁体   中英

Can you use the self argument in a static method in python class?

I'm making a sqlite3 db and in that, I have a static method that shows all the tables present in the sqlite3 file, but for that file to access the cursor object it needs the self.conn.cur, and if make the cur a global var all the other methods have to be changed so can I use the self arg in the static method? Pls Help. Thanks in advance.

No, a static method will not get any reference to self - self is the object the method is being invoked on, and in a static method, there is no object. If the method isn't static - ie it requires an object with a connection set, don't define it as static and call it on an object instead.

Another option is to call the static method and provide the connection as an argument. That allows the calling code to determine which connection should be used to retrieve information when called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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