str is the class name of all strings in Python. str.lower is one of his methods.
If you call lower in one of your instances (for example, 'ABC'.lower() ), you call the bound method, which automatically sends the called object as the first argument (usually called self ).
If you call lower in the class itself (i.e. use str.lower() ), you call the unbound method , which does not automatically provide the self argument. Therefore, you must specify an object in order to act independently.
If all this seems difficult to understand, it will be easier when you consider how methods are defined in classes. Let them say that we create our own very simple class that represents a point (X, Y coordinate in space). And it has a show() method to print the dot.
class Point: """This is the constructor of Point""" def __init__(self, x, y):
Please note that we did not need to specify the argument self (therefore p.show () was called without arguments). In fact, the previous call was more or less equivalent to this:
Point.show(p)
They are not quite equivalent, but this is a more advanced topic. One of the simplest cases when they will not be equivalent is if you change the p.show value after creating the object, for example:
p.show = 4
Now p.show() will not even compile, since p.show is no longer a function, but an integer! However, Point.show(p) will remain unchanged since we modified the show attribute in an instance of class ( p ), and not in the class itself ( Point ).
source share