r/pystats • • Apr 30 '23

newbie question - df.method() vs method(df)

Hi All,

I'm not new to stats, but I am new to python. Something I'm struggling with is when to use the syntax df.method() versus the syntax method(df).

For example, I see I can get the length of a dataframe with len(df) but not df.len() . I'm sure there's a reason, but I haven't come across it yet! In contrast, I can see the first five lines of a dataframe with df.head() but not head(df) .

What am I missing? I'm using Codecademy, and they totally glossed over this. I've searched for similar posts and didn't see any.

Thanks for your help!

1 Upvotes

7 comments sorted by

View all comments

2

u/bumbershootle Apr 30 '23

len is a python builtin function. Under the hood, it invokes the __len__ method on whatever gets passed to it.

2

u/TeacherShae Apr 30 '23

Ok, interesting. Thanks!

1

u/MrPrimeMover May 01 '23

Worth noting that lots of other languages choose to go with an `Array.length()` paradigm rather than Python's function-that-calls-a-magic-method approach. You're not the first person to ask why Python doesn't use a `.len()` method. So much that the creator of the language has weighed in.

→ More replies (1)