Python Tutorial
Python Tutorial
Methods
are part of a programming philosophy called Object-Oriented Programming (OOP).
While OOP is built into Python by design, remember that you can productively use
Python as an ordinary programming language and only incorporate the concepts of OOP
when you need them.
Here's a brief overview of OOP:
• The basis of OOP is that, as a programmer, you should focus on the data
you are working with, whether it is the results of a scientific experiment, counts
of hits on a Web page, or the contents of your personal CD collection.
• Instead of keeping track of this data in separate locations, it is
incorporated into an object, which contains all the information you would need to
know to operate on the data.
• Methods are then constructed that know how to operate on those objects.
Since the object contains all the available information, it's not necessary to
assemble any additional information to invoke the method.
Furthermore, when you invoke a method on an object, Python figures out which method
to run, because it recognizes the object as having special properties. So, for
example, if you had a CD collection and a tape collection, you could create a print
method for each, and when you want to print the contents of a tape or CD, simply
invoke it by name -- Python will figure out which method to use for which object.
Invoking a method on an object is similar to invoking a function, but instead of
including the object in the argument list as with a regular function, you precede
the method call with the name of the object followed by a period. Thus, to invoke
the print method for our fictitious CD collection on a CD object called album1, we
would type
album1.print()
instead of
print(album1)
Additional arguments to methods, if present, would be passed to the method just as
they would be for functions.