0

I see there are a few ways to perform a method to an object. Either a.method(), method(a).

Here's an example,

import pandas as pd
data = {'AXP': 86.40, 'CSCO': '122.64', 'BA': '99.44'}
sindex = ['AXP', 'CSCO', 'BA', 'AAPL',]
aSer = pd.Series(data, index = sindex)

# Ex1: First way
> aSer.isnull()
AXP     False
CSCO    False
BA      False
AAPL     True
dtype: bool

# Ex2: Second way
> pd.isnull(aSer)
AXP     False
CSCO    False
BA      False
AAPL     True
dtype: bool

# Ex3: third way
> aSer.isnull
<bound method NDFrame.isnull of AXP       86.4
CSCO    122.64
BA       99.44
AAPL       NaN
dtype: object>

# Ex4: function vs. method 
aList = [3, 5, 2, 4]
sorted(aList) # object in function
aList.sort()  # method

My understanding:
Ex1: "attaches" a method to the object.

Ex2: put the object inside a function. Are first two ways always equivalent?

Ex3: what is it doing here?

Ex4: Why aList.sorted() won't work? sorted() vs sort() are from different package.

I am confused why so many things try to achieve the same thing, at the same time, not all of them will work.

user13985
  • 221
  • 1
  • 10
  • 2
    Short answer: No. Longer and arguably more correct answer: It depends on the implementations of `x.method` and `method`. – Christian Dean Aug 16 '17 at 21:12
  • Probably a bad habit inherited from numpy. – wim Aug 16 '17 at 21:12
  • I am an R user. I think Python is quite confusing. Too many things going on. – user13985 Aug 16 '17 at 21:45
  • The link is good, but, I'm a beginner. I don't want paragraphs of definition, and don't understand complicated code. Just In this context, with these examples. That's all, thanks! – user13985 Aug 16 '17 at 23:13
  • 1
    This is a very basic programming question you can answer yourself after learning more about class methods and functions. Then look in the source code to see what these do. Python is a true programming language, R is not. In order to have a true understanding you need to grasp the "paragraphs of definition" as you call it. – Andrew L Aug 16 '17 at 23:59
  • The "true language" makes things more complicated. In R, each function is associated with one task. You don't get aList.sorted(), vs sort(aList). sort(aList) is for sorting, that's it! Simple and elegant. WHY Python has two? It seems Python lack unity, each developer does his own thing. – user13985 Aug 17 '17 at 00:43
  • No, it is not answer in the link above! – user13985 Aug 17 '17 at 15:01

0 Answers0