Reputation: 762
Suppose i have a data-frame object named as df, head() is a method that can be applied to df to see the first 5 records of the data-frame and df.size is a property to get the size of the data-frame.
For the property we are not using '()' as we used for a method. This was little confusing initially. Could anyone explain whats basic difference between a property and a method in python. I mean why we had to define size as a property for a dataframe, why not it was defined as a method which would have just returned the size of the data-frame.
Upvotes: 0
Views: 590
Reputation: 38
Here in the above example you mentioned, you can pass argument to df.head()
function, where as you cannot pass arguments for properties.
for same above example, if you have written df.head(20)
it would return first 20 rows.
Upvotes: 0
Reputation: 54
So I thought I'd link you to this answer because I think this explains it pretty well.
https://www.tutorialspoint.com/What-is-the-difference-between-attributes-and-properties-in-python
If I had to explain it in my own words, properties are the 'attributes' of the object that have methods (get, set and delete).
So, if I understand correctly, df.size will be your property and then you need to define get, set and delete methods for this property. Also, I would recommend reading through the python class docs just to get proper usage and definitions.
https://docs.python.org/3/tutorial/classes.html
Upvotes: 1