As an example: math.sqrt()
is considered a function but, "Lower".upper()
is known as the .upper()
method.
Why is math.sqrt
a function? Isn't the dot access indicative of methods?
As an example: math.sqrt()
is considered a function but, "Lower".upper()
is known as the .upper()
method.
Why is math.sqrt
a function? Isn't the dot access indicative of methods?
Because math
is a module*, not a class.
sqrt
is a function defined inside the module math
. You can access objects that have been defined in a module by normal dot .
access on the module object. With modules though, no transformation of the function object occurs during that access, the object returned as is.
Specifically, since functions are descriptors, their __get__
isn't invoked when accessed through the module.
upper
is a function that's defined inside the str
class. When you invoke it on a str
instance using dot access, it is transformed into a method which implicitly receives the instance as a first argument.
Specifically, when accessed through a class, a function object's __get__
is invoked which, in turn, transforms the function into a method type.
*Okay, I hate to do this, because it is confusing, but modules are also classes. They are special in that they don't do the transformation as described previously. Specifically, their getattribute function that is invoked when an attribute is requested, doesn't act like the default for classes.