• Using Classes In Python - Part 2


    Creating the class definition

    A class need not be particularly complex. In fact, you can create just the container and one class element and call it a class. Of course, the resulting class won’t do much, but you can instantiate it (tell Python to build an object using your class as a blueprint) and work with it as you would any other class. The following steps help you understand the basics behind a class by creating the simplest class possible.


    Open a Python Shell window.

    You see the familiar Python prompt.

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

    class MyClass: MyVar = 0

    The first line defines the class container, which consists of the keyword class and the class name, which is MyClass. Every class you create must begin precisely this way. You must always include class followed by the class name.

    The second line is the class suite. All the elements that comprise the class are called the class suite. In this case, you see a class variable named MyVar, which is set to a value of 0. Every instance of the class will have the same variable and start at the same value.

     Type MyInstance = MyClass( ) and press Enter.

    You have just created an instance of MyClass named MyInstance. Of course, you’ll want to verify that you really have created such an instance. Step 4 accomplishes that task.

     Type MyInstance.MyVar and press Enter.

    The output of 0, demonstrates that MyInstance does indeed have a class variable named MyVar.



     Type MyInstance.__class__ and press Enter.

    Python displays the class used to create this instance. The output tells you that this class is part of the __main__ module, which means that you typed it directly into the shell.


    6. Retain this window and class for the next section.

     

    Considering the built-in class attributes


    When you create a class, you can easily think that all you get is the class. However, Python adds built-in functionality to your class. For example, in the preceding section, you type __class__ and press Enter. The __class__ attribute is built in; you didn’t create it. It helps to know that Python provides this functionality so that you don’t have to add it. The functionality is needed often enough that every class should have it, so Python supplies it. The following steps help you work with the built-in class attributes.

     Use the Python Shell window that you open in the preceding section.

    If you haven’t followed the steps in the preceding section, “Creating the class definition,” please do so now.

     Type dir(MyInstance) and press Enter.

    A list of attributes appears. These attributes provide specific functionality for your class. They’re also common to every other class you create, so you can count on always having this functionality in the classes you create.

     Type help(‘__class__’) and press Enter.

    Python displays information on the __class__ attribute. You can use the same technique for learning more about any attribute that Python adds to your class.

     Close the Python Shell window.

    Working with methods


    Methods are simply another kind of function that reside in classes. You create and work with methods in precisely the same way that you do functions, except that methods are always associated with a class (you don’t see freestanding methods as you do functions). 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. The following sections provide the details needed to work with both.


    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 in order to modify strings. The following steps demonstrate how to create and use a class method.

     Open a Python Shell window.

    You see the familiar Python prompt.

     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.

     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.

     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.


     Open a Python Shell window.

    You see the familiar Python prompt.

     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.

     Type MyInstance = MyClass( ) and press Enter.

    Python creates an instance of MyClass named MyInstance.

     Type MyInstance.SayHello( ) and press Enter.


    5. Close the Python Shell window.
  • 0 comments:

    Post a Comment

    Powered by Blogger.

    Tags

    Popular Posts