简体   繁体   English

Python:使用 object 作为参数(例如方法(XXX)与以下语法:XXX.method())之间有区别吗?

[英]Python: is there a difference between using an object as an argument e.g. method(XXX) vs. this syntax: XXX.method()?

Sorry that I do not yet have the vocabulary to express this question properly.抱歉,我还没有正确表达这个问题的词汇。

For example:例如:

# Import sas7bdat package
from sas7bdat import SAS7BDAT

# Save file to a DataFrame: df_sas
with SAS7BDAT('sales.sas7bdat') as file:
    df_sas = file.to_data_frame()

# Print head of DataFrame
print(df_sas.head())

and

# Save file to a DataFrame: df_sas
with SAS7BDAT('sales.sas7bdat') as file:
    df_sas = SAS7BDAT.to_data_frame(file)

# Print head of DataFrame
print(df_sas.head())

both produce the same result.两者都产生相同的结果。 Are they generally equivalent or is this a special circumstance?它们通常是等效的还是特殊情况?

I wouldn't call it a special case but rather indirect.我不会称其为特殊情况,而是间接的。

You are creating an object file that is an instance of that class SAS7BDAT with with SAS7BDAT('sales.sas7bdat') as file: and then you are using the Class method to_data_frame from the Class instead from the instance directly. You are creating an object file that is an instance of that class SAS7BDAT with with SAS7BDAT('sales.sas7bdat') as file: and then you are using the Class method to_data_frame from the Class instead from the instance directly.

It is really a special circumstance and maybe the second code has another side-effect you cant see yet.这确实是一种特殊情况,也许第二个代码还有另一个你看不到的副作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不熟悉的 Python 语法(例如 obj1 =<class method> (<method parameters> )(obj2))</method></class> - Unfamiliar Python syntax (e.g. obj1 = <class method>(<method parameters>)(obj2)) 检测Python代码的运行位置(例如,在Spyder解释器与IDLE与cmd之间) - Detect where Python code is running (e.g., in Spyder interpreter vs. IDLE vs. cmd) 如何使用计算方法(例如使用Python)找到以下所需概率? - How to find the following desired probability using computational method (e.g. using Python)? python 中自适应文件路径的最佳方法是什么? 例如使用 pd.read_csv() - What is the best method for an adaptive file path in python? e.g. using pd.read_csv() 以 "Seconds.microseconds" 格式获取时间戳 PYTHON 之间的时差 [例如 0.123456 秒] - Get Time Difference between Timestamps PYTHON in the format "Seconds.microseconds" [e.g. 0.123456 sec] Python slice如何区分slice参数和默认参数(例如x [i:]与x [i:None])? - Python slice how to distinguish slice arguments and default arguments (e.g., x[i:] vs. x[i:None])? python中的多处理-在多个进程之间共享大对象(例如pandas数据帧) - multiprocessing in python - sharing large object (e.g. pandas dataframe) between multiple processes Python 字典与数据库(例如 SQL 数据库) - Python Dictionary vs Database (e.g. an SQL Database) Python中的object.method()和method(object)之间的区别? - Difference between object.method() and method(object) in Python? 数字后面的包含点的数组(例如[1. 2.]与没有点的数组(例如[1 2])和有什么区别? - What is the difference between and array containing dot after the number e.g. [ 1. 2. ] and an array without dot e.g. [ 1 2 ]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM