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

User-defined types的更多相关文章

  1. [C++]Linux之头文件sys/types.h[/usr/include/sys]

    1.查找<sys/types.h>文件 一般地,Linux的C头文件<sys/types.h>路径在如题的途径:/usr/include/sys下,然而博主[Linux For ...

  2. Go语言规格说明书 之 类型(Types)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...

  3. 【ToolKit】轻量级JS库

    优点: 丢弃了一些不常用的方法(jQuery.fn):slideUp.fadeIn.animate等: 新增获取子节点的方法(ToolKit.fn):firstChild,lastChild等: 新增 ...

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

    虽然golang是用C实现的,并且被称为下一代的C语言,但是golang跟C的差别还是很大的.它定义了一套很丰富的数据类型及数据结构,这些类型和结构或者是直接映射为C的数据类型,或者是用C struc ...

  5. gdb 常用内容

    gdb exegdb exe coregdb -p info m TAB ^関数の先頭 info b ^list the breakpoint set args -a test ^引数設定 show ...

  6. JPA 教程

    Entities An entity is a lightweight persistence domain object. Typically an entity represents a tabl ...

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

    一. 分区表理论知识 Oracle提供了分区技术以支持VLDB(Very Large DataBase).分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中.分区完全对应用透明. Orac ...

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

    虽然golang是用C实现的,并且被称为下一代的C语言,但是golang跟C的差别还是很大的.它定义了一套很丰富的数据类型及数据结构,这些类型和结构或者是直接映射为C的数据类型,或者是用C struc ...

  9. oracle分区提高篇

      一. 分区表理论知识 Oracle提供了分区技术以支持VLDB(Very Large DataBase).分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中.分区完全对应用透明. Or ...

  10. Hex-Rays decompiler type definitions and convenience macros

    /****************************************************************************************** Copyrigh ...

随机推荐

  1. IntelliJ IDEA启动spring boot项目出现Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]

    IntelliJ IDEA启动spring boot项目出现Failed to start component [StandardEngine[Tomcat].StandardHost[localho ...

  2. HDU 5410(2015多校10)-CRB and His Birthday(全然背包)

    题目地址:HDU 5410 题意:有M元钱,N种礼物,若第i种礼物买x件的话.会有Ai*x+Bi颗糖果,现给出每种礼物的单位价格.Ai值与Bi值.问最多能拿到多少颗糖果. 思路:全然背包问题. dp[ ...

  3. sqlite学习笔记11:C语言中使用sqlite之删除记录

    最后一节,这里记录下怎样删除数据. 前面全部的代码都继承在这里了,在Ubuntu14.04和Mac10.9上亲測通过. #include <stdio.h> #include <st ...

  4. 《从零開始学Swift》学习笔记(Day 46)——下标重写

    原创文章.欢迎转载.转载请注明:关东升的博客 下标是一种特殊属性. 子类属性重写是重写属性的getter和setter訪问器,对下标的重写也是重写下标的getter和setter訪问器. 以下看一个演 ...

  5. Woody的Python学习笔记2

    Python多行语句 Python语句中一般以新行作为语句的结束符.但我们能够使用斜杠(\)将一行的语句分为多行显示,例如以下所看到的: total = item_one+\ item_two + \ ...

  6. bzoj3442: 学习小组(费用流好题)

    3442: 学习小组 题目:传送门 题解: 超级好题啊大佬们的神题!建图肥肠灵性!感觉自己是星际玩家... 首先呢st直接向每个人连边,容量为min(k,喜欢的小组个数),费用为0 然后每个人再向ed ...

  7. <%=%>、<%%>、<%@%>、<%#%>的区别

    1.<%= %> 里面放变量名,获取后台的变量值,直接输入变量到页面上,里面放的变量名,未经过encode eg: 后台: seession["ab"]=ab; 前台: ...

  8. BootStrap学习(二)——重写首页之topbar

    1.布局容器 帮助文档:http://v3.bootcss.com/css/#overview-container BootStrap需要为页面内容和栅栏系统包裹一个.container容器.提供的两 ...

  9. Eclipse使用struts2开发web应用快速搭建

    eclipse 下载javaEE版,这里用4.5(Mars). Tomcat8解压,设置好JAVA_HOME环境变量. 下载struts2官网上的lib包,struts-2.3.24-lib.zip, ...

  10. (四)React高级内容

    1. React developertools安装及使用 2. PropTypes与DefaultProps 讲一下PropTypes, 先拿TodoItem来说: 从几种类型中选: 3 props ...