【问题标题】:cant "connect" classes in my sublime text 3 python code无法在我的崇高文本 3 python 代码中“连接”类
【发布时间】:2018-04-07 22:12:07
【问题描述】:

我在 Sublime Text 3 中编程,我需要在另一个类中使用一个面向对象的类,但我无法从另一个类访问它及其功能(我将它们都保存在我计算机上的同一目录中) ,谢谢各位帮忙。

【问题讨论】:

标签: python python-3.x python-2.7 class sublimetext3


【解决方案1】:

如果我理解正确,我认为您正试图从另一个 python 模块访问一个 python 模块(每个“模块”都是一个文件),并且每个模块都包含一个类。

如果A类是Foo,保存在foo.py,B类是Bar,保存在bar.py,你想在Foo里面使用Bar,那么你需要@987654328 @Foo 以便在定义 Bar 时在范围内。

您可以尝试在bar.py 中,上面定义了Bar:from foo import Foo

如需更多信息,这可能会有所帮助:https://docs.python.org/3/tutorial/modules.html

【讨论】:

  • 谢谢,但我已经尝试导入该类,但它无法识别它及其功能。
【解决方案2】:

类1:

import sqlite3 

from driver import Driver

from sqlitte import error

def create():

try:
    conn = sqlite3.connect(':memory:')

except error as e:
    print (e)


c = conn.cursor()
c.excute("""CREATE TABLE users(
    price real,
    discount real
    )""")

conn.commit()

conn.close()

和类2(与driver.py保存在同一目录中):

class Driver:

def __init__(self, online_id):
    self.online_id = online_id


def get_online_id():
    return self.online_id

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 2014-06-09
    • 2016-12-21
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多