Ch. 9 Lists and Dictionaries (Python3)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Class Attribute

A class attribute is shared amongst all of the instances of that class. Class attributes are defined within the scope of a class. See image:

Rich Comparison Methods

Methods like __lt__ above are known as rich comparison methods. The following table describes rich comparison methods and the corresponding relational operator that is overloaded. See image:

unfinished

xxx

Abstract Data Type (ADT)

A class can be used to implement the computing concept known as an abstract data type (ADT), which is a data type whose creation and update are constrained to specific, well-defined operations (the class interface). A key aspect of an ADT is that the internal implementation of the data and operations are hidden from the ADT user, a concept known as information hiding, which allows the ADT user to be more productive by focusing on higher-level concepts. Information hiding also allows the ADT developer to improve the internal implementation without requiring changes to programs using the ADT.

Attributes

A class creates a new type of object. The object maintains a set of attributes that determines the data and behavior of the class. For example, the following code defines a new class containing two attributes, hours and minutes, whose values are initially 0: See image:

Adding parameters to a constructor

A class instance is commonly initialized to a specific state. The __init__ method constructor can be customized with additional parameters, as shown below: See image:

Class interfaces

A class interface consists of the methods that a programmer calls to create, modify, or access a class instance. The figure below shows the class interface of the RaceTime class, which consists of the __init__ constructor and the print_time() and print_pace() methods. See image:

Method

A method is a function defined within a class. The __init__ method, commonly known as a constructor, is responsible for setting up the initial state of the new instance. See image: In the example above, the __init__ method creates two new attributes, hours and minutes, and assigns default values of 0.

Class & Instance Object

A program with user-defined classes contains two additional types of objects: class objects and instance objects. A class object acts as a factory that creates instance objects. When created by the class object, an instance object is initialized via the __init__ method. Class and instance objects are namespaces used to group data and functions together. Instance and class namespaces are linked to each other. If a name is not found in an instance namespace, then the class namespace is searched.

Multiple Instances

A programmer can create multiple instances of a class in a program, with each instance having different attribute values. See image:

9.3 Chapter term recap:

Attribute: a name following a "." symbol Self: a method parameter that refers to the class instance Instance: an instantiation of a class __init__: a constructor method that initializes a class instance class: a group of related variables and functions

Instance Attribute

An instance attribute can be different between instances of a class. See image:

Instantiation Operation

An instantiation operation is performed by "calling" the class, using parentheses like a function call as in my_time = Time(). An instantiation operation creates an instance, which is an individual object of the given class. An instantiation operation automatically calls the __init__ method defined in the class definition.

Object

An object consists of some internal data items plus operations that can be performed on that data. For example; RunnerInfo object supports operations like setting distance, setting time, and computing speed.

Accessing Attributes

Attributes can be accessed by using the "attribute reference operator" also called "dot notation." See image:

Operator Overloading

Class customization can redefine the functionality of built-in operators like <, >=, +, -, and * when used with class instances, a technique known as operator overloading.

Class customization

Class customization is the process of defining how a class should behave for some common operations. Such operations might include printing, accessing attributes, or how instances of that class are compared to each other. To customize a class, a programmer implements class methods with special method names that the Python interpreter recognizes. For example, to change how a class instance object is printed, the special __str__() method can be defined, as illustrated below. See image:

9.4 Chapter term recap

Class object: a factory for creating new class instances Instance object: represents a single instance of a class Instance methods: functions that are also class attributes Instance attribute: a variable that exists in a single instance Class attribute: a variable shared with all instances of a class.

Creating Instance Attributes

Created using dot notation, as in self.speed = 7.5 within a method, or runner1.speed = 7.5 from outside of the class' scope.

Constructor

Is responsible for setting up the initial state of the new instance.

Multiple Variables

Multiple variables are frequently closely related and should thus be treated as one variable with multiple parts. For example; two variables called hours and minutes might be grouped together in a single variable called time.

'Built-ins' Operations

Python automatically creates certain objects, known as built-ins, like ints, strings, and functions.

Class keyword

The class keyword can be used to create a user-defined type of object containing groups of related variables and functions. See image.

Special Method Name

The method implements some special behavior of the class. __init__ is a special method name. Special methods can always be identified by the double underscores __ that appear before and after an identifier.

Method Object

The python object that encapsulates the method's code. A method object is an attribute of a class, and can be referenced using dot notation. See image:

Self

a Method parameter that automatically references the instance being created. A programmer writes an expression such as self.hours = 5 within the __init__ method to create a new attribute hours.


Set pelajaran terkait

Human Resources BUSI 342 Retaining and Recruiting Human Capital Assignment

View Set

Parts of the Brain involved in memory

View Set

Nurs 327 musculoskeletal (final chapters)

View Set

Personal Finance Final Questions

View Set