- Learning Object-Oriented Programming
- Gastón C. Hillar
- 550字
- 2021-07-16 13:46:05
Declaring classes in Python
Throughout this book, we will work with Python 3.4.3. However, all the explanations and code samples are compatible with Python 3.x.x. Therefore, you can work with previous Python versions as long as the major version number is 3. We will use JetBrains PyCharm Community Edition 4 as the main Python IDE and the supplier of an interactive Python console. However, you can use your favorite Python IDE or just the Python console.
Note
Everything is a class in Python, that is, all the elements that can be named in Python are classes. Guido van Rossum designed Python according to the first-class everything principle. Thus, all the types are classes, from the simplest types to the most complex ones: integers, strings, lists, dictionaries, and so on. This way, there is no difference between an integer (int
), a string
, and a list
. Everything is treated in the same way. Even functions, methods, and modules are classes.
For example, when we enter the following lines in a Python console, we create a new instance of the int
class. The console will display <class 'int'>
as a result of the second line. This way, we know that area
is an instance of the int
class:
area = 250 type(area)
When we type the following lines in a Python console, we create a new instance of the function
class. The console will display <class 'function'>
as a result of the second line. Thus, calculateArea
is an instance of the function
class:
def calculateArea(width, height): return width * height type(CalculateArea)
Let's analyze the simple calculateArea
function. This function receives two arguments: width and height. It returns the width value multiplied by the height value. If we call the function with two int
values, that is, two int
instances, the function will return a new instance of int
with the result of width
multiplied by height
. The following lines call the calculateArea
function and save the returned int
instance in the rectangleArea
variable. The console will display <class 'int'>
as a result of the third line. Thus, rectangleArea
is an instance of the int
class:
rectangleArea = calculateArea(300, 200) print(rectangleArea) type(rectangleArea)
The following lines create a new minimal Rectangle
class in Python:
class Rectangle: pass
The class
keyword followed by the class name (Rectangle
) and a colon (:
) composes the header of the class definition. In this case, the class doesn't have a parent class or a superclass. Therefore, there aren't superclasses enclosed in parentheses after the class name and before the colon (:
). The indented block of statements that follows the class definition composes the body of the class. In this case, there is just a single statement, pass
, and the class doesn't define either attributes or methods. The Rectangle
class is the simplest possible class we can declare in Python.
Tip
Any new class you create that doesn't specify a superclass will be a subclass of the builtins.object
class. Thus, the Rectangle
class is a subclass of builtins.object
.
The following line prints True
as a result in a Python console, because the Rectangle
class is a subclass of object
:
issubclass(Rectangle, object)
The following lines represent an equivalent way of creating the Rectangle
class in Python. However, we don't need to specify that the class inherits from an object because it adds unnecessary boilerplate code:
class Rectangle(object): pass
- 高手是如何做產品設計的(全2冊)
- 深入淺出Java虛擬機:JVM原理與實戰
- Java從入門到精通(第4版)
- C語言程序設計教程(第2版)
- PostgreSQL技術內幕:事務處理深度探索
- VMware vSphere 6.7虛擬化架構實戰指南
- 數據結構簡明教程(第2版)微課版
- Python進階編程:編寫更高效、優雅的Python代碼
- Ext JS 4 Web Application Development Cookbook
- 學習正則表達式
- Visual C#通用范例開發金典
- Flutter跨平臺開發入門與實戰
- Python算法詳解
- RealSenseTM互動開發實戰
- Building Machine Learning Systems with Python(Second Edition)