![]() |
This is an Object - Code interpretation - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: This is an Object - Code interpretation (/thread-33994.html) |
This is an Object - Code interpretation - JoeDainton123 - Jun-16-2021 Hello all I am trying to gain a better understanding of object orientation programming. I cam across the following code:- import pandas as pd df = pd.read_csv("T.csv") print(df.head(2))This code works and I replaced the actual csv file with my own. My question is would it be correct to say that: 1) df is an object of the class read_csv 2) The class read_csv is found in an imported module 3) The imported module is given the alias pd Is this correct? RE: This is an Object - Code interpretation - Yoriz - Jun-16-2021 (Jun-16-2021, 06:50 PM)JoeDainton123 Wrote: 1) df is an object of the class read_csv df is a variable that has a pointer to an instance of a pandas.DataFrame class(Jun-16-2021, 06:50 PM)JoeDainton123 Wrote: 2) The class read_csv is found in an imported module read_csv is a function of the imported pandas module(Jun-16-2021, 06:50 PM)JoeDainton123 Wrote: 3) The imported module is given the alias pdIf the module name is followed by as, then the name following as is bound directly to the imported module. RE: This is an Object - Code interpretation - snippsat - Jun-16-2021 (Jun-16-2021, 06:50 PM)JoeDainton123 Wrote: I am trying to gain a better understanding of object orientation programming.Pandas is maybe not the best to look at as it really big and dos a lot specialization. Yoriz explain it fine. Normally so would read_csv() by a method of the class,pandas just write it as a function,then has special @Appender decorator.@Appender( _doc_read_csv_and_table.format( func_name="read_csv", summary="Read a comma-separated values (csv) file into DataFrame.", _default_sep="','", storage_options=generic._shared_docs["storage_options"], ) ) def read_csv( filepath_or_buffer: FilePathOrBuffer, sep=lib.no_default, delimiter=None, # Column and Index Locations and Names header="infer", .....ect If i gone write a demo as more normal OOP class way. # my_pandas.py class MyPandas: def read_csv(self, file): with open(file) as f: print(f.read()) def head(self, file): with open(file) as f: print(f.read()[:20])Use: >>> import my_pandas as pd >>> >>> df = pd.MyPandas() >>> df.read_csv('Articles.csv') name;regcode;number Salah;111;22 ali;222;33 Ranard;333;44 >>> >>> df.head('Articles.csv') name;regcode;numberYou see the similarity way of usage now is read_csv() and head() methods of class MyPandas.They are not function as pandas use in there specialization way. |