Beginning Programming with Python For Dummies
Book image
Explore Book Buy On Amazon
Methods are simply another kind of function that reside in classes. You create and work with methods in Python in precisely the same way that you do functions, except that methods are always associated with a class. You can create two kinds of methods: those associated with the class itself and those associated with an instance of a class. It’s important to differentiate between the two.

Creating class methods

A class method is one that you execute directly from the class without creating an instance of the class. Sometimes you need to create methods that execute from the class, such as the functions you used with the str class to modify strings. The following steps demonstrate how to create and use a class method.
  1. Open a Python Shell window.

    You see the familiar Python prompt.

  2. Type the following code (pressing Enter after each line and pressing Enter twice after the last line):

    class MyClass:
     def SayHello():
      print("Hello there!")

    The example class contains a single defined attribute, SayHello(). This method doesn’t accept any arguments and doesn’t return any values. It simply prints a message as output. However, the method works just fine for demonstration purposes.

  3. Type MyClass.SayHello() and press Enter.

    The example outputs the expected string. Notice that you didn’t need to create an instance of the class — the method is available immediately for use.

    Working with methods in Python
  4. Close the Python Shell window.

A class method can work only with class data. It doesn’t know about any data associated with an instance of the class. You can pass it data as an argument, and the method can return information as needed, but it can’t access the instance data. As a consequence, you need to exercise care when creating class methods to ensure that they’re essentially self-contained.

Creating instance methods

An instance method is one that is part of the individual instances. You use instance methods to manipulate the data that the class manages. As a consequence, you can’t use instance methods until you instantiate an object from the class.

All instance methods accept a single argument as a minimum, self. The self argument points at the particular instance that the application is using to manipulate data. Without the self argument, the method wouldn’t know which instance data to use. However, self isn’t considered an accessible argument — the value for self is supplied by Python, and you can’t change it as part of calling the method.

The following steps demonstrate how to create and use instance methods in Python.
  1. Open a Python Shell window.

    You see the familiar Python prompt.

  2. Type the following code (pressing Enter after each line and pressing Enter twice after the last line):

    class MyClass:
     def SayHello(self):
      print("Hello there!")

    The example class contains a single defined attribute, SayHello(). This method doesn’t accept any special arguments and doesn’t return any values. It simply prints a message as output. However, the method works just fine for demonstration purposes.

  3. Type MyInstance = MyClass() and press Enter.

    Python creates an instance of MyClass named MyInstance.

  4. Type MyInstance.SayHello() and press Enter.

    You see this message.

    Instance methods in Python
  5. Close the Python Shell window.

About This Article

This article is from the book:

About the book author:

John Paul Mueller is a freelance author and technical editor with more than 107 books and 600 articles to his credit. His subjects range from networking and artificial intelligence to database management and heads-down programming. He also consults and writes certification exams. Visit his website at http://www.johnmuellerbooks.com/.

This article can be found in the category: