The Method Function & Class in Python
Novice Python programmers need to learn about object-oriented programming before they can begin fully utilizing the language's potential. This means learning about Python system of writing the code for classes, using classes to instantiate independent data structures called objects, then executing segments of code called methods within a given object to perform specialized tasks.
-
Classes and Objects
-
When a Python programmer writes a class he is making a blueprint for the Python interpreter to follow for constructing a specific kind of data structure, also known as an object. Each time the programmer instantiates an instance object of a particular class each one will start off with the same variables, structure and functionality. However, they will be independently functioning. Consequently, if a programmer modifies a variable in one object, the other objects he instantiated from the same class will be unchanged.
Methods
-
A class contains a series of individual functions called methods. Each method will contain the code to perform a specific operation. This operation can be as simple as modifying one of its object's variables or displaying a line of text. Alternatively, the operation could be one of many complex equations that drive a statistical simulation. At the end of a method, the programmer can choose to have the method "return" some value to the main program. Variables and values that are stored or calculated within a given object are usually encapsulated within that object, away from the immediate access of the main program. However, returning such a value gives the main program access to it.
-
Arguments
-
Just as the main program does not have immediate access to variables within a given object, a given object does not have immediate access to variables from the main program. Similarly, just as object methods can pass values to the main program by returning them the main program can pass values to object methods through "arguments." When a Python programmer writes a method declaration that follows the syntax "myMethod()" then the method cannot accept any arguments. However, if he writes the declaration as "myMethod(argument1, argument2)" then the main program can pass in two arguments. The programmer can access these values in the body of the method by referencing "method1" or "method2" as variables.
Calling Methods
-
When a Python programmer wants to call a specific method from a particular object he follows the formula "objectName.methodName()" where "objectName" is the name he gave the particular object when he instantiated it from the class, and "methodName" is the name of the particular method. Using the correct object name is important because the different objects that instantiated from a single class operate independently. If a programmer wants to call a method that modifies a value which "ObjectA" is storing but calls the same method from "ObjectB," then the program will change the value within ObjectB and not within ObjectA.
-
References
- Dive Into Python; Mark Pilgram
- Python Documentation: The Python Tutorial: 9. Classes
- Photo Credit Comstock/Comstock/Getty Images