0

Very basic question.

I’m a newbie to python here and had a question with some syntax. 
I’ve noticed two different ways of running commands on data in Python.

In one the location is specified first and then the command is run. In other words going from left to right you first specify the location and then the function that is to be run. For example:

CustomerBasicInfo['MeatLikelyhood'].plot.hist()

In this I would say that the location is:

CustomerBasicInfo['MeatLikelyhood'].

And the function is:

plot.hist()

However, alternatively there are certain scenarios in which the the function comes first and the location is specified later. For example:

sum(DfPayments['Amount'])

The function sums is first followed by the location

(DfPayments['Amount’])

I was just wondering why this is so. And if this is because I'm overlooking something can you please tell me what I can read to brush up on my theory?

Regards

2 Answers2

2

In the first case CustomerBasicInfo['MeatLikelyhood'] is an object and the plot.hist() is being called as a method.

"A method in object-oriented programming is a procedure associated with a class." ~https://study.com/academy/lesson/oop-object-oriented-programming-objects-classes-interfaces.html

In the second case, sum is a function, and "DfPayments['Amount']" is an object- more specifically iterable.

A function takes in a parameter as input and acts on that. A method is called on an object and acts on that.

Vikhyat Agarwal
  • 1,723
  • 1
  • 11
  • 29
0

In general, if you have a process that can be applied to multiple types of objects, it will be implemented as a function. When the process is specific to a certain type of object, it will generally be a method of the object's class.

For example, sum() applies to all types of objects that can be iterated over: lists, tuples, sets, iterators, ranges, etc. so it is implemented as a general purpose function.

On the other hand .append() is a method of the list object class because its behaviour and effect is specific to lists. Sets use have the .add() method, ranges cannot be added to, dictionaries use subscripting, etc.

Alain T.
  • 40,517
  • 4
  • 31
  • 51