How to: Python Class Methods

Save

A class is an important structure used in many object-oriented programming languages like Python. Class objects contain variables and functions designed to imbue them with specialized properties and behaviors. A class definition is a block of code that defines the attributes of a class. With a class definition in place, a class may be instantiated to create an object with the properties and behavior specified in its definition. Writing a class definition is quite like creating your own data type. The functions contained in a class object are called methods, and you declare them just like other Python functions, with a few special conditions to remember.

  • Open a plain text editor to write a new Python class. In Windows, click the Start Menu, choose "All Programs," click "Accessories" and then click "Notepad."

  • Start a class definition with the following command:

    class myClass:

    As with all Python code blocks, entab the following lines to make them part of the class definition.

  • Define the special "init" method to execute code whenever a new instance of the class is created. This is like a constructor in C++.

    def init(self, initialvalue1, initialvalue2):

    This special method's name is surrounded by double underscores to prevent namespace conflicts. In contrast with other languages, members of Python classes cannot truly be made "private" or "public." Nevertheless, the underscoring convention is used to reflect the same principles.

  • Add the following lines, double-entabbed to make them part of the "init" method:

    self.value1 = initialvalue1

    self.value2 = initialvalue2

    The "self" argument will automatically contain a reference to the class object when it is created. The other two arguments will be specified when a class object is instantiated. "Value1" and "value2" are variables that each class object will contain.

  • Add a blank line to terminate the "init" method. Define another method on its own entabbed line:

    def showValues(self):

  • Double-entab and type the following commands:

    print(self.value1)

    print(self.value2)

    Add a blank line to terminate this method and the class declaration.

  • Save your class definition to the default Python module directory (C:\Python32\Lib on a typical Windows installation) using the filename "myclass.py."

  • Launch the Python command-line interpreter.

  • Import the class definition you wrote with the following command:

    import myclass

  • Create a new instance of "myClass" with the following command:

    f = myclass.myClass(5,6)

    The module and class names are case-sensitive, so type the command exactly as shown. The variable "f" now contains an instance of "myClass."

  • Invoke the "showValues" method with the following command:

    f.showValues()

    The values "5" and "6" are printed to the command line. You can instantiate as many "myClass" objects as you like, each with unique values for "value1" and "value2."

  • Add multiple methods to your class definition to give your class objects specialized behavior. Class methods may change values in the class object, invoke other methods, and much more.

References

Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!