A small thing nobody taught you in Python
Have you ever noticed that some stuffs like lower()
, upper()
, sort()
etc. are called like this variable.lower()
or variable.sort()
. But most of the other function are called like this len(variable)
, str(variable)
.
What’s the difference?
Actually, the first set of examples are actually called attributes or methods.
Let’s take the example of lower()
, which acts on the str
datatype.
str
datatype is actually a class and has a number of functions inside it. These functions are called attribute and can be accessed by using the .
notation.
|
|
So, back to our example:
|
|
On the other hand, the latter examples were all functions, like your normal def func()
thing. They perform a certain task on the argument that is passed to them. The task may or may not be unique to a particular datatype.
|
|
I hope you leaned something new today 😄.