User-defined types

We have used many of Python’s built-in types; now we are going to define a new type. As an example, we will create a type called Point that represents a point in two-dimensional space.

In mathematical notation, points are often written in parentheses with a comma separating the coordinates. For example, (0,0) represents the origin, and (x,y) represents the point x units to the right and y units up from the origin.

There are several ways might represent points in Python:

  • We could store the coordinates separately in two variables, x and y.
  • We could store the coordinates as elements in a list or tuple.
  • We could create a new type to represent points as objects.

Creating a new type is more complicated than other options, but it has advantages that will be apparent soon. A user-defined type is also called a class. A class definition looks like this:

This header indicates that the new class is called Point. The body is a docstring that explains what the class is for.

Because Point is defined at the toplevel, its ‘full name’ is __main__.Point. The class object is like a factory for creating objects. To create a Point, you call Point as if it were a function. The return value is a reference to a Point object, which we assign to blank. Creating a new object is called instantiation, and the object is an instance of the class.

from Thinking in Python

时间: 2024-10-17 00:57:35

User-defined types的相关文章

golang: 常用数据类型底层结构分析

虽然golang是用C实现的,并且被称为下一代的C语言,但是golang跟C的差别还是很大的.它定义了一套很丰富的数据类型及数据结构,这些类型和结构或者是直接映射为C的数据类型,或者是用C struct来实现.了解golang的数据类型和数据结构的底层实现,将有助于我们更好的理解golang并写出质量更好的代码. 基础类型 源码在:$GOROOT/src/pkg/runtime/runtime.h .我们先来看下基础类型: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1

SQL Server on Linux: How? Introduction: SQL Server Blog

SQL Server Blog Official News from Microsoft's Information Platform https://blogs.technet.microsoft.com/dataplatforminsider/2016/12/16/sql-server-on-linux-how-introduction/ This post was authored by Scott Konersmann, Partner Engineering Manager, SQL

一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计 转载

文章来源 http://www.cnblogs.com/surpassal/archive/2012/10/09/Zynq_Lab4.html 本小节通过使用XPS中的定制IP向导(ipwiz),为已经存在的ARM PS 系统添加用户自定IP(Custom IP ),了解AXI Lite IP基本结构,并掌握AXI Lite IP的定制方法,为后续编写复杂AXI IP打下基础.同时本小节IP定制方法同样适用于MicroBlaze处理系统. 本小节定制的是简单LED的IP,只有一个数据寄存器,向其

PLSQL Coding Standard

Naming and Coding Standards for SQL and PL/SQL "The nice thing about standards is that you have so many to choose from." - Andrew S Tanenbaum Introduction This document is mentioned in a discussion on the OTN forums. One of the first comments be

导出类成员里含有stl对象

How to export an instantiation of a Standard Template Library (STL) class and a class that contains a data member that is an STL object Summary This article discusses how to perform the following tasks: Export an instantiation of a Standard Template

【GoLang】golang底层数据类型实现原理

虽然golang是用C实现的,并且被称为下一代的C语言,但是golang跟C的差别还是很大的.它定义了一套很丰富的数据类型及数据结构,这些类型和结构或者是直接映射为C的数据类型,或者是用C struct来实现.了解golang的数据类型和数据结构的底层实现,将有助于我们更好的理解golang并写出质量更好的代码. 基础类型 源码在:$GOROOT/src/pkg/runtime/runtime.h .我们先来看下基础类型: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1

Shallow copy and Deep copy

一.来自wikipidia的解释: Shallow copy One method of copying an object is the shallow copy. In that case a new object B is created, and the fields values of A are copied over to B. This is also known as a field-by-field copy,field-for-field copy, or field co

Oracle 分区表的新增、修改、删除、合并。普通表转分区表方法

一. 分区表理论知识 Oracle提供了分区技术以支持VLDB(Very Large DataBase).分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中.分区完全对应用透明.Oracle的分区表可以包括多个分区,每个分区都是一个独立的段(SEGMENT),可以存放到不同的表空间中.查询时可以通过查询表来访问各个分区中的数据,也可以通过在查询时直接指定分区的方法来进行查询. When to Partition a Table什么时候需要分区表,官网的2个建议如下: Tables g

Boost 1.61.0 Library Documentation

http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for incremental calculation, and collection of statistical accumulators. Author(s): Eric Niebler First Release: 1.36.0 Standard: Categories: Math and nume

Programmer Competency Matrix

Note that the knowledge for each level is cumulative; being atlevel n implies that you also know everything from thelevels lower than n. Computer Science   2n (Level 0) n2 (Level 1) n (Level 2) log(n) (Level 3) Comments data structures        Doesn’t