简体   繁体   中英

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.

It is really a special circumstance and maybe the second code has another side-effect you cant see yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM