Page 409 - Computer_Science_F5
P. 409

Computer Science  In  Python, the class keyword is used   number 12 and the string “Hello, world”
           to  define  and  create  classes. Attributes
                                                     are both objects.
           refer to the variables that are associated
                                                     An object consists of:
           with a particular class. In this scenario,
                                                     (a)  State:  It is represented by the
           attributes  are  always  public  and  can
          FOR ONLINE READING ONLY
           be accessed  using the  dot (.) operator.
                                                         the properties of an object.
           For instance, if we have a class called       attributes of an object. It also reflects
           MyClass, and it  possesses  an  attribute   (b)  Behavior: It is represented by the
           named MyAttribute, we can access it by        methods of an object. It also reflects
           using the syntax MyClass.MyAttribute.         the response of an object to other
                                                         objects.
           You can create an empty class, Program    (c)  Identity: It gives a unique name to
           Example 6.1 shows how you can create          an object and enables one object to
           empty class of Dog,                           interact with other objects.

                    Program Example 6.1:                 To understand the state, behavior,
                                                         and identity take the example of the
            Creating an empty class in Python            class dog that was provided before. In
                                                         the context of a dog, the identity can
                  class Dog:                             be thought of as the dog’s name.

                   pass                                  The state or attributes of a dog refer
                                                         to characteristics such as its breed,
                                                         age, or color. The behavior of a dog
           In this example, Dog is a class with          encompasses actions like eating or
           no attributes  or methods.  It serves as      sleeping.
           a blueprint that can be used to create
           objects with no initial state or behavior.
                                                     Creating class and object in Python
           Python objects                            Classes serve as blueprints for creating

           In Python’s object-oriented programming   objects,  encapsulating    both   data
           paradigm, an object represents an entity   (attributes) and behavior (methods). To
           that possesses both state and behavior.   define a class, use the “class” keyword,
                                                     followed by the class name and a colon
           These objects can be any real-world item   to start the class block.
           such as a mouse, keyboard, chair, table,
           pen, and so on.  Additionally, objects    Creating objects (Instances)
           can be data types like integers, strings,   Objects are instances of classes,
           floating-point  numbers,  arrays,  and    representing individual entities based on
           dictionaries. In fact, even simple entities   the class blueprint. To create an object,
           like a single integer  or a single string   instantiate the class by calling the class
           are considered objects. For example, the   name followed by parentheses.

                                                 400
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   400                                                     23/07/2024   12:34
   404   405   406   407   408   409   410   411   412   413   414