Python Forum
This is an Object - Code interpretation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
This is an Object - Code interpretation
#1
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?
Reply
#2
(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 pd
If the module name is followed by as, then the name following as is bound directly to the imported module.
snippsat likes this post
Reply
#3
(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;number
You 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  vars() can't be used in list interpretation? Cheng 3 2,064 Jun-22-2021, 11:59 AM
Last Post: snippsat
  English interpretation of the following file handing snippet mortch 5 3,120 May-30-2019, 08:10 AM
Last Post: mortch
  Code review for S3 object creation beherap 0 2,115 Mar-29-2018, 01:31 PM
Last Post: beherap
  The number of object makes code slower? fig0 1 2,467 Jan-25-2018, 11:16 PM
Last Post: Gribouillis
  Interpretation of a code Alberto 1 2,083 Jan-02-2018, 06:46 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020