You can think of the object.method()
dot notation as being shorthand for class.method(object)
where the object instance of a class is implicitly passed as a parameter to the method. For example a common string method is lower()
to return a lowercased version of the string object, and you could use it a couple ways. The first you're probably more used to:
>>> s = 'TEST'
>>> s.lower()
'test'
s
is defined to be a str
object and it looks like lower()
doesn't take any parameters. But what's really happening is:
>>> str.lower(s)
'test'
and you're calling the string method lower
on the string object s
If you wanted to make your own class that also used the object.method()
notation with a similar lower method then you would need to define lower with a self
parameter-def lower(self):
so that the method knew which object to act on. And the variable self
is not a keyword in python. It can be named anything you want but the first parameter will allow the method to be called on the object directly.