我是中级Python学习者
我正在学习Python中用于数据科学的编码类,特别是现在学习如何导入数据集。我遇到的一个常见问题(我将在下面的示例中显示)是,我将被告知对一些变量(如“engine”)使用类似“table_names()”的方法
我经常犯的错误是不知道这是否意味着我的代码应该是:
engine.table_names()
或者这个:
table_names(engine)
视情况而定,这两种选择似乎都是可能的
在这种情况下,有没有办法立即知道正确答案是这样的
engine.table_names()
基本上,我已经学会了做很多不同的事情,但这类事情仍然困扰着我。我甚至不知道如何正确地问我的问题,所以希望有人能理解我的问题
下面是完整的练习:
Import the function create_engine from the module sqlalchemy.
Create an engine to connect to the SQLite database 'Chinook.sqlite' and assign it to engine.
Using the method table_names() on the engine engine, assign the table names of 'Chinook.sqlite' to the variable table_names.
Print the object table_names to the shell.
以下是正确的代码:
# Import necessary module
from sqlalchemy import create_engine
# Create engine: engine
engine = create_engine('sqlite:///Chinook.sqlite')
# Save the table names to a list: table_names
table_names = engine.table_names()
# Print the table names to the shell
print(table_names)
通常在方法和函数之间进行区分
engine.table_names()
是一种方法table_names(engine)
将是一个函数相关问题 更多 >
编程相关推荐