1/23
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
object-based programming
primarily create and use objects of existing classes
Classes
new data types
When creating a new class, instead of writing all new code, you can
designate that the new class is to be formed initially by inheriting the attributes (variables) and methods (the class version of functions) of a previously defined base class (also called a superclass). The new class is called a derived class (or subclass). After inheriting, you then customize the derived class to meet the specific needs of your application
base class or a superclass
a previously defined base class
derived class or a subclass
new class
inheritance
allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class)
polymorphism
enables you to conveniently program “in the general” rather than “in the specific.” You simply send the same method call to objects possibly of many different types
Each new class you create becomes
a new data type that can be used to create objects
from file import Class
use the import command followed by the module name and then the class name
constructor expression
it builds and initializes an object of the class. create new objects and initialize their data using argument(s) specified in parentheses
class.attribute
access the class object’s attributes
class header
A class definition begins with the keyword class followed by the class’s name and a colon (:). The Style Guide for Python Code recommends that you begin each word in a multi-word class name with an uppercase letter
To view any class’s docstring in IPython, type the
class name and a question mark, then press Enter
docstring
Each class typically provides this. When provided, it must appear in the line or lines immediately following the class header
def init(self, attributeparameter)
Each new class you create can provide an __ method that specifies how to initialize an object’s data attributes
Returning a value other than None from __init__ results in a
TypeError. Recall that None is returned by any function or method that does not contain a return statement
When you call a method for a specific object, Python implicitly passes a
reference to that object as the method’s first argument. For this reason, all methods of a class must specify at least one parameter
By convention most Python programmers call a method’s first parameter
self. A class’s methods must use that reference (self) to access the object’s attributes and other methods
self.attribute_name = value
A class's attributes are added dynamically via assignments of this form
Unlike class methods, what cannot validate the values you assign to them?
data attributes
Such data in these languages is said to be private data
code that uses objects of the class.
encapsulate
hide an object’s data from the client code. This creates private data, which Python doesn't have. Instead, you use naming conventions to design classes that encourage correct use
any attribute name beginning with an underscore (_) is for a
class’s internal use only
Attributes whose identifiers do not begin with an underscore (_) are considered
publicly accessible for use in client code