-3

After going through some lectures on class and objects, what I have understood is that if a function is a part of a class then it can be accessed by object which is known as method and if a function is outside a class, then it can be accessed directly.

So for example -

1)

def introduce():
        return 'My name is Joseph'

student1 = introduce()

Here since introduce is not within any class , we directly used introduce function and the result shall be 'My name is Joseph' which shall be stored in student1

2)

class room():
    def introduce(self):
        return 'My name is Joseph'

student1 = introduce()
a = student1.introduce()

Here this introduce function is inside room class and therefore to use it we have to associate a object with a class and then use it

Now going by this lets take an example of Pandas

we have a function named as - pandas.read()

I think pandas is a class but then here does it mean that pandas is a object and a read() is a function inside pandas ? Am I missing something ? Shouldn't this be the case ?

df = pandas() # making an object of pandas class
df.read() # then accessing the read function inside pandas class

Kindly assist me

Joseph arasta
  • 161
  • 1
  • 3
  • 12
  • 2
    Pandas is module and read() is function in it. Just like you had room.py file and then accessed you introduce function by `room.introduce()`. – Dawid Gacek May 24 '21 at 06:32
  • Just as you said, `df` is basically an object (or an instance) of the `pandas` class. That’s also what’s written in the comments in your code? – MoPaMo May 24 '21 at 06:33
  • 1
    What's your issue? – Paul Lemarchand May 24 '21 at 06:33
  • 1
    "I think pandas is a class" — If your code includes the line `import pandas` then pandas is a module, not a class. You can `print(type(pandas))` to see what it is. – khelwood May 24 '21 at 06:35
  • Why do we write pd.read() and not df.read(). Why don't we associate any object with pandas class in general ? – Joseph arasta May 24 '21 at 06:36
  • 1
    @Josepharasta I'm not sure what you mean. I would write df.read() if df is an instance of pandas. – Paul Lemarchand May 24 '21 at 06:39
  • I think those are two different things: In the one hand you’ve got static functions, which you call directly on the class name (like `pd.read()`), and in the other hand there are *normal* methods you call on an object, like `pd.read` – MoPaMo May 24 '21 at 06:41
  • This question might help you: https://stackoverflow.com/a/114267/12907088 (Someone already posted that link but deleted it again) – MoPaMo May 24 '21 at 06:46
  • 1
    `pandas` is not a class. – user2357112 May 24 '21 at 06:57
  • @user He’s obviously just beginning coding, so that’s a bit too specific for him, I’d say – MoPaMo May 24 '21 at 07:18
  • @mpm: What? Are you talking to me? Are you seriously saying that "`pandas` is not a class" is somehow *too specific* for a beginner? – user2357112 May 24 '21 at 08:10

1 Answers1

1

There are 3 types of functions(methods) you can have inside an python class.

  1. Instance Method
  2. Class Method
  3. Static Method

Here's a thing to see that what you have created the method named introduce inside room that is "Instance Method". Which takes object as self parameter meaning which can be accessed by Instance(object).

You can also define a Class Method in following way.

class room():
    @classmethod
    def introduce(self):
        return 'My name is Joseph'

This can be accessed as

room.introduce()

If you want to know about more about what is static or class methods. Visit here Check out

Note: The website has no connection to me I just found this online and I liked it so shared it ;)

Parshwa
  • 46
  • 5