Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从其他类访问一个类中的方法_Python - Fatal编程技术网

Python 如何从其他类访问一个类中的方法

Python 如何从其他类访问一个类中的方法,python,Python,我正试图编写一个代码来解析一个类中的一些数据,然后将解析后的数据插入到sqlite DB和其他类中的其他任务中 我创建了一个sqlite类: class DB_MNG(object): def db_conn(self): self.conn = sqlite3.connect('db.sqlite') self.cur = self.conn.cursor() self.cur.execute(

我正试图编写一个代码来解析一个类中的一些数据,然后将解析后的数据插入到sqlite DB和其他类中的其他任务中

我创建了一个sqlite类:

 class DB_MNG(object):

        def db_conn(self):

            self.conn = sqlite3.connect('db.sqlite')
            self.cur = self.conn.cursor()
             self.cur.execute(
                    'CREATE TABLE IF NOT EXISTS hosts(ip TEXT PRIMARY KEY 
                  UNIQUE, port INTEGER, details'
                    ' TEXT, details2 INTEGER)')

      def db_operator(self, ip, port, details, details2):
            self.ip = ip
            self.details = details
            self.port = port
            self.details2 = details2
            self.cur.execute('SELECT ip FROM hosts WHERE ip = ? ', 
            (self.ip,))
            self.raw = self.cur.fetchone()
            self.conn.commit()

    class data_parser(DB_MNG):
    def __init__(self):
    DB_MNG.__init__(self)

我需要将一些已解析的数据传递到DB_MNG.DB_operator并相应地更新表…

尝试以下操作:

class Test1():
    def f1(self):
        print('Test1->f1()')

class Test2():
    def __init__(self):
        self.obj1 = Test1()
        self.obj1.f1()
首先创建类Test1,然后在类Test2中创建Test1类的对象

然后,在从Test2类创建对象的过程中,您可以从第一个类运行方法:

obj2 = Test2()
>> Test1->f1()

考虑到这是python,请花一些时间适当缩进代码,因为它会改变问题的含义