【问题标题】:Importing variables from another python file从另一个 python 文件导入变量
【发布时间】:2021-04-17 20:33:29
【问题描述】:

我正在尝试将一个 python 文件中定义的变量导入另一个文件中,两者都在同一个目录中。 以下是示例:

# filename1

class Proton:

 def test(self):

   self.a_tags = soup_file.find_all('a')
   self.cases = [str(each.get_text()) for each in self.a_tags]
   self.links = [(link.get('href')) for link in soup_file.find_all('a')]

# I want to import the above values in the below file

# filename2

import sqlite3

class Database:

    def __init__(self):

        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = sqlite3.connect('gopel.db')
        self.curr = self.conn.cursor()

    def create_table(self):

        self.curr.execute('''CREATE TABLE testfall(cases TEXT, links TEXT)''')

    def testfall_db(self):

        self.curr.execute('''INSERT INTO testfall VALUES(?,?)''',(self.cases,self.links,))
        self.curr.execute('''SELECT cases FROM testfall WHERE cases=?''', [self.cases])
        self.conn.commit()

我在 filename2 中使用了from filename1 import *,但这些值仍未定义,并且在数据库中它存储 NULL 值。关于如何将这些导入到 filename2 文件的任何建议。

PS:filename1 和 filename2 是两个不同的文件

【问题讨论】:

  • 你只导入sqlite3...请提供minimal reproducible example的代码和你得到的错误
  • filename1 中定义的唯一变量是Proton。您还希望导入哪些其他变量?
  • 您不能导入类属性。在testfall_db() 中,selfDatabase,而不是Proton,所以你不能在那里访问self.cases。你需要创建一个Proton,然后你才能访问它的cases属性。
  • 您似乎期望self.casesProton 实例的变量,可以在Database 的实例中访问。
  • 您似乎对对象的工作方式存在根本性的误解。

标签: python


【解决方案1】:

好问题!

这里的主要问题是您在 Proton 类中定义的变量是 Proton 对象的“实例变量”(也称为字段或数据成员)。另一种思考方式是它们“属于”某个 Proton 实例。

如果不先创建那个对象,想一想,它们就不存在了!

在下面的例子中,我们首先初始化一个 Proton 对象,然后访问它的一个实例变量。

my_proton = Proton() # Creating a Proton object

my_proton.a_tags # Accessing the a_tags attribute

下面,我编辑了您的代码,以便您可以访问 Proton 对象的实例变量。在下面的示例中,我刚刚创建了测试标签来展示如何访问它们。

# filename1: "proton.py"  

class Proton:

  def __init__(self):
    self.a_tags = []
    self.cases = []
    self.links = []

  def test(self):
    self.a_tags = ['test a_tags']
    self.cases = ['test cases']
    self.links = ['test links']

# filename2: "database.py"

import sqlite3
from proton import Proton # from the file named "proton", import the Proton class

class Database:

  my_proton = Proton() # create a Proton object
  print(my_proton.a_tags) # access the a_tags object from the Proton object and print

【讨论】:

    【解决方案2】:

    假设您已导入 Proton,如果您希望它以最小的更改工作:

    def testfall_db(self):
    
        p = Proton() # need to create the instance first
        p.test()     # call the test method to initialize those values
    
        # access those values from the `p` instance of Proton created above
        self.curr.execute('''INSERT INTO testfall VALUES(?,?)''',(p.cases,p.links,))
        self.curr.execute('''SELECT cases FROM testfall WHERE cases=?''', [p.cases])
        self.conn.commit()
    

    例如,您的代码中的self.cases 正在调用不存在的数据库实例的cases 变量-> None

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 2021-06-22
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多